Solved

Python if statement

Posted on 2015-01-13
2
174 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
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. …
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…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now