[英]What am I doing wrong, I'm not sure how to fix my code
我正在尝试在python中进行数学测验,而当我输入正确的答案后,大多数代码都可以正常工作时,代码始终无法正确打印。
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=="+":
您正在将List对象与字符串进行比较。 好像错了 正如乔恩(Jon)所述,这导致answer
始终保持为0。
你应该换成这样
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")
跟踪当前操作是什么,并使用该值进行比较
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.