Solved

Python if statement

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

627 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