954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is wrong with this code?

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

jackbauer24
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 21
Solved Threads: 2
 

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?

jackbauer24
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 21
Solved Threads: 2
 
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
 

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.

jackbauer24
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 21
Solved Threads: 2
 

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. http://docs.python.org/tutorial/
http://docs.python.org/tutorial/introduction.html
http://wiki.python.org/moin/BeginnersGuide

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
Moderator
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
 
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)
jackbauer24
Posting Whiz in Training
234 posts since Oct 2011
Reputation Points: 21
Solved Threads: 2
 

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
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: