Solved

Python if statement

Posted on 2015-01-13
2
206 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
2 Comments
 
LVL 16

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 28

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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