0

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?

  • you need to convert the input to an integer. also your code will accept 6 as correct for "3 - 2" and 12 as correct for "6 * 6" for example – Sean Nov 21 '15 at 00:05
  • 1
    I suggest there should be a canonical question for the "my Python math quiz homework doesn't work" question. Possible duplicate of [a](http://stackoverflow.com/questions/33455047/python-arithmetic-quiz-task-1), [b](http://stackoverflow.com/questions/33232374/how-do-i-check-if-the-user-has-entered-a-number), [c](http://stackoverflow.com/questions/33221287/creating-a-quiz-in-python-error), [d](http://stackoverflow.com/questions/33178117/whats-wrong-with-my-code-invalid-syntax) ... – TessellatingHeckler Nov 21 '15 at 00:07

1 Answers1

0

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

It tried a few time and got the correct output. However, I don't think the following behavior is accepted, right?

rand1 = 3
rand2 = 4
signlist = '+'

result = 7 works, as well as result = 12 and result = -1.

You should check the result depending of signlist:

from random import randint

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

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

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

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

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

Because you're not allowed to return something. When you use print, you don't return anything, you just print something, and this is accepted.

Yuriko
  • 576
  • 3
  • 14
  • I am quite inexperienced with "or" & "and" functions, clearly.They were only briefly explained to our class last week before having this task thrust our way. Thank you very much for the help, Yuriko. Although, I have just ran your code 4 times, still to be output "incorrect" all four times. The formatter %s also seemed to be causing trouble as well, bring up an "invalid syntax" error – silk_spectrum Nov 21 '15 at 00:36
  • After a bit of editing, its working. Brilliant. Thank you very much! – silk_spectrum Nov 21 '15 at 00:49
  • [*and*, *or* are not functions, they are operators.](http://www.tutorialspoint.com/python/python_basic_operators.htm "Tutorialspoint - Python Basic Operators") – Yuriko Nov 21 '15 at 08:07