vendredi 20 novembre 2015

python random sum quiz

i am writing a program in school for a certain task in which 10 random questions will be output, each time prompting the user for an answer. below is the code i have worked on so far:

from random import randint

rand1 = (randint(0,12))
rand2 = (randint(0,12))

signlist = ["x", "+", "-"]

print(rand1, signlist[randint(0,2)], rand2)
result = input("what is the answer to the sum above? ")

if result == rand1+rand2 or result == rand1-rand2 or result ==rand1*rand2:
    print ("correct")
else:
    print ("incorrect")

regardless on whether or not the user inputs the correct answer, the program outputs "incorrect".

is it due to the 'or' function? am i using it incorrectly? or does anyone suggest completely rewriting the code (if it is that inaccurate )

i was also having trouble using 'return' within my if statement, as the following error message occurred:

'return' outside function.

why is this error message showing when 'print' is replaced with return?

Aucun commentaire:

Enregistrer un commentaire