You don't seem to understand how if statements, nor general syntax in Python works.
In Python, we mark blocks of code by
some_statement:
some code
code not in block
That's it. Just indent - there's no need to follow your statements up with "break". Remove your breaks.
Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
But I want my program to stop if the ages are above 100. In that case how do I do it?
Have a look at the code here - http://docs.python.org/tutorial/controlflow.html#if-statements
And read it. Then, have a look at your code, and try to see what you're doing wrong. It'll be quite obvious if you're diligent in your reading.
Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
break command is used to exit the current for or while statement from middle of loop.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
I get it.The program will break even if I don't put "break." Then what's the use of break? Sorry if I am irritating you.
No, you're not annoying me, you've just not understood. I suggest you read some more tutorials and see if they help you :)
Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
Okay.Need a little more help.
Is there something wrong with this?
import time
import sys
print """Enter your name
"""
myname = chr(sys.stdin.readline())
print """Hello %s. How are you today?
Would you mind entering your age?:
"""% myname
myanswer = (sys.stdin.readline())
if myanswer == "yes":
myage = int(sys.stdin.readline())
print "Hello %s. You are %s years old."%(myname, myage)
time.sleep(5)
sys.exit()
else:
print "Suit yourself. Bye %s!"% myname
sys.exit(5)
Is there something wrong ? is not a good question. Good questions areDoes it fail with exception ? (then study the error message)
Does it produce the expected result ? (if not, work more)
Could the code be improved ? (usually yes, but it depends on your knowledge of python)
Here I suggest something like
import time
import sys
myname = raw_input("""Enter your name: """).strip()
print """Hello %s. How are you today?""" % myname
myanswer = raw_input("Would you mind entering your age? [yes] ").strip().lower()
if myanswer in ('', 'y', 'yes'):
myage = raw_input()
while True:
try:
myage = int(myage.strip())
break
except ValueError:
myage = raw_input("Please enter a numeric age: ")
print "Hello %s. You are %s years old." % (myname, myage)
time.sleep(5)
sys.exit()
else:
print "Suit yourself. Bye %s!"% myname
sys.exit(5)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691