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
# 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
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 random
statement 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.