Solved

Python if statement

Posted on 2015-01-13
2
234 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:9apit
[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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
JavaScript Cross Reference Tool 4 146
python pyusb how to find a file on mass storage 3 98
Python:Remove duplicates without sorting? 5 117
Ruby or Python 7 134
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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 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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

752 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