I am trying to make a maths quiz in python and while most of the code works when I type in the correct answer the code always prints incorrect.
import random
Name=input("What is your name?")
Class=input("wWhat class are you in?")
print("Welcome" ,Name, "to the Maths Quiz!!")
QuestionNumber=0
Operations=["+","-","x"]
Num1=random.randint(1,30)
Num2=random.randint(1,30)
answer=0
while QuestionNumber < 10:
QuestionNumber=QuestionNumber+1
print("What is", Num1 ,random.choice(Operations),Num2)
guess=int(input("What is the answer to this question?"))
if Operations=="+":
answer=Num1+Num2
elif Operations=="-":
answer=Num1-Num2
elif Operations=="x":
answer=Num1*Num2
if guess==answer:
print ("Correct")
else:
print("Incorrect")
if Operations=="+":
you are comparing a List object with a string. That seems wrong. As jon mentions, this causes answer
to always stay 0.
You should change to something like this
while QuestionNumber < 10:
QuestionNumber=QuestionNumber+1
operation = random.choice(Operations)
print("What is", Num1 ,operation, Num2)
guess=int(input("What is the answer to this question?"))
if operation =="+":
answer=Num1+Num2
elif operation =="-":
answer=Num1-Num2
elif operation =="x":
answer=Num1*Num2
if guess==answer:
print ("Correct")
else:
print("Incorrect")
Keep track of what the current operation is and use that value to compare
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.