### Thread: Newbie programming on python

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
8
Rep Power
0

#### 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. 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:
```import random

# 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()```

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.
Last edited by Schol-R-LEA; June 1st, 2013 at 04:31 PM.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
8
Rep 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.