Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python if statement

Posted on 2015-01-13
2
Medium Priority
?
267 Views
Last Modified: 2015-01-13
Hello experts,

Working on using IF statement in Python
Tutorial at
https://docs.python.org/2/tutorial/controlflow.html#if-statements

>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...     x = 0
...     print 'Negative changed to zero'
... elif x == 0:
...     print 'Zero'
... elif x == 1:
...     print 'Single'
... else:
...     print 'More'
...
More

This has the input and the output so removed output and got

x = int(raw_input("Please enter an integer: "))
if x < 0:
     x = 0
     print 'Negative changed to zero'
    elif x == 0:
     print 'Zero'
     elif x == 1:
     print 'Single'
     else:
     print 'More'

When I run the code in the IDE
it prompts for the integer But then
It does nothing just the >>> 

Why does it not execute the code after the
input?

Thanks

Allen In Dallas
0
Comment
Question by:Allen Pitts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
gelonida earned 2000 total points
ID: 40547673
Probably you just forgot to type one empty line at the end of the if statement.

Be careful, that you keep the correct indentation if not the code will not do what you want it to do.

what you should have typed is:

x = int(raw_input("Please enter an integer: "))
if x < 0:
     x = 0
     print 'Negative changed to zero'
elif x == 0:
     print 'Zero'
elif x == 1:
     print 'Single'
else:
     print 'More'

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 40547675
First, it is difficult to judge. I suggest to save the code into a file with .py extension and launch it from the file using the python interpreter. Check the indentation.

(Modified later) I have just tried:
c:\_Python\9apit\Q_28596003>py
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...     x = 0
...     print 'Negative changed to zero'
... elif x == 0:
...     print 'Zero'
... elif x == 1:
...     print 'Single'
... else:
...     print 'More'
...
More
>>>

Open in new window

The first line must be entered first. Then it is immediately interpreted. It asks you for intput and fills the x variable. Then you have to type in the if command. As gelonida wrote, the last line with three dots must be entered empty. This way the interactive mode recognizes that you finished the command.

When you type x (nothing more + Enter), the interactive mode will display the content of the variable for you.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question