Math quiz program in python- newbie needs help
### i am supposed to write a program that gives simple math quizzes- the program should display two random numbers to be added. What am I doing wrong?
# import random function because i am using it
# main function
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!)
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
June 28th, 2013, 02:42 PM
Well seeing as how this is a homework question, or some form of assignment, it's not going to HELP you become a programmer by giving you the answer but I ran and fixed your code, so I'll give you steps which will help you problem solving skills a little bit and help you find these errors as they are mostly syntax errors.
First, your program had quite a few syntax errors in it and those won't let the program run anyways. One hint would be to make sure new line characters are in double quotes "\n" because Python doesn't know what that character means when it's not placed into it's proper context. Also you should try a reference site, such as the Python documentation to help you figure out how to format output in Python.
Also, in future posts you should document the version of Python you are using as well as a small description of the problem you are having. So go back into the IDE and run the program, you should see output from Python telling you the program line and the problem. Start with that, and if your still having problems come back.
Also, a small challenge, this program only generates a single question, how might you rewrite your code to let the program continue asking questions and getting responses?