Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 1st, 2013, 03:25 PM
 lailamalu
Registered User

Join Date: May 2013
Posts: 8
Time spent in forums: 35 m 48 sec
Reputation Power: 0
Homework - Newbie programming on python

###What am I doing wrong? This is a math quiz that gives 2 random numbers to add.
# main function
def main():
print("This program prints 2 random numbers between 1 and 2000 and asks you to add them.")
print("Type the answer after you see the 2 random numbers.")
#generate 2 different numbers to add together
num1 = random.randint(1,2000)
num2 = random.randint(1,2000)
# print and add the two integers
total = sum(num1, num2)
# print the equation on 3 lines
print(num1, \n, "+",\n, num2))
# if statement
print("You are correct!)
else:
print("Try again." \n, "Correct answer is: " \n, format(num3, '.2f'))
def sum(num1, num2):
return num1 + num2

# call main function
main()

#2
June 1st, 2013, 04:25 PM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,800
Time spent in forums: 1 Month 2 Weeks 3 Days 2 h 27 m 30 sec
Reputation Power: 1569
Well, first off, your posting in the wrong forum - there's a separate forum for Python, if you scroll up to the first "Programming Languages" group. But that's beside the point.

Not so beside the point is that you need to put your code samples in [code] or [highlight] tags, so that the indentation is preserved. This is particularly important for Python, where the indentation and formatting are actually significant parts of the program structure. You can either enter the tags manually, or you can do it by pressing the "#" or the highlight marker buttons at the top of the editing window.
python Code:
 Original - python Code
```import random # main functiondef main():    print("This program prints 2 random numbers between 1 and 2000 and asks you to add them.")    print("Type the answer after you see the 2 random numbers.")    #generate 2 different numbers to add together    num1 = random.randint(1,2000)    num2 = random.randint(1,2000)    # print and add the two integers    # correct answer to equation    total = sum(num1, num2)    # print the equation on 3 lines    print(num1, \n, "+",\n, num2))    # asking for the user to input answer    answer = int(input("Enter answer to math problem: "))    # if statement    if total == answer:        print("You are correct!")    else:        print("Try again." \n, "Correct answer is: " \n, format(num3, '.2f')) # defining the addition function def sum(num1, num2):    return num1 + num2                 # call main function    main()
```

As for what is wrong, part of it is that the indentation was off in places, which would cause a nesting error. Next, you omitted the import randomstatement at the beginning of the program. Also, you dropped the colon in the if: statement, which makes for another syntax error. Finally, you dropped a double-quote ending one of the strings in a print call. I have fixed these issues in the code above.

There may be other problems that aren't so obvious; we would need you to tell us what is going wrong, what error messages are coming up, and so forth. If the program fails or crashes, post the traceback. If it runs but is incorrect, tell us about the incorrect behavior. We can just look at a program and blindliy guess what is happening when it is run.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : June 1st, 2013 at 04:31 PM.

#3
June 2nd, 2013, 12:29 PM
 lailamalu
Registered User

Join Date: May 2013
Posts: 8
Time spent in forums: 35 m 48 sec
Reputation Power: 0
thank you- i forgot that my tabs/indents did not roll over when I transferred this file. i will try the python forum next time. thanks for help.

 Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Homework - Newbie programming on python