What is wrong with this code?

import time
import sys
print """Welcome to the age finder.
This program takes some input (they will be the years) and then
adds it to 4 ages. For example, if you give the ages 10, 11, 12
and 13 it will return the age added to your first input.
"""
# These are the years to add.
print "Enter the number of years to be added:"
yearstoadd = int(sys.stdin.readline())
# These are the ages to add.
print "Enter the first age:"
num1 = int(sys.stdin.readline())
if num1 > 100:
    print "Sorry, this is a very big age"
break
else:    
    print "Enter the second age:"
    num2 = int(sys.stdin.readline())
    if num1 > 100:
        print "Sorry, this is a very big age"
    break
    else: 
        print "Enter the third age:"
        num3 = int(sys.stdin.readline())
        if num1 > 100:
            print "Sorry, this is a very big age"
        break
        else:     
            print "Enter the fourth age:"
            num4 = int(sys.stdin.readline())
            if num1 > 100:
                print "Sorry, this is a very big age"
            break    
            else: 

                num1 = num1 + yearstoadd
                num2 = num2 + yearstoadd
                num3 = num3 + yearstoadd
                num4 = num4 + yearstoadd
                print "The new ages are %s, %s, %s and %s"% (num1, num2, num3, num4)
                time.sleep(10)

When I run this code, this error appears:
C:\Documents and Settings\Administrator\Desktop>agefinder.py
File "C:\Documents and Settings\Administrator\Desktop\agefinder.py", line 17
else:
^
SyntaxError: invalid syntax

Member Avatar for Enalicho

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.

But I want my program to stop if the ages are above 100. In that case how do I do it?

Member Avatar for Enalicho

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.

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.

The Python break key word is a way to break out of a loop. Just that. So, for instance

result=None
for i in range(1000000000): # really long loop
    result = check_item(i)
    if are_we_done_looking(i, result):
        break # so we don't have to finish the loop
if result:
    generate_report(result)
else:
    print("Useful result not found.")

Please go read an introduction to Python programming. For instance these, though there are probably hundreds.

break command is used to exit the current for or while statement from middle of loop.

Member Avatar for Enalicho

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 :)

Quote originally posted by thepythonguy ...
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

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)

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 are

  • Does 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)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.