Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我做错了什么,我';我不知道如何修复我的代码_Python_Python 3.x - Fatal编程技术网

Python 我做错了什么,我';我不知道如何修复我的代码

Python 我做错了什么,我';我不知道如何修复我的代码,python,python-3.x,Python,Python 3.x,我正在尝试用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

我正在尝试用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")
随机导入
Name=输入(“你叫什么名字?”)
Class=input(“您在哪个班?”)
打印(“欢迎”,姓名,“参加数学测验!!”)
问题编号=0
操作=[“+”、“-”、“x”]
Num1=random.randint(1,30)
Num2=随机的随机数(1,30)
答案=0
当问题编号<10时:
问题编号=问题编号+1
打印(“是什么”,Num1,随机选择(操作),Num2)
guess=int(输入(“这个问题的答案是什么?”)
如果操作==“+”:
答案=Num1+Num2
elif操作==“-”:
答案=Num1-Num2
elif操作==“x”:
答案=Num1*Num2
如果猜测==答案:
打印(“正确”)
其他:
打印(“不正确”)
如果操作==“+”:

您正在将列表对象与字符串进行比较。这似乎是错误的。正如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")
当问题编号<10时:
问题编号=问题编号+1
操作=随机。选择(操作)
打印(“是什么”,Num1,操作,Num2)
guess=int(输入(“这个问题的答案是什么?”)
如果操作==“+”:
答案=Num1+Num2
elif运算==“-”:
答案=Num1-Num2
elif操作==“x”:
答案=Num1*Num2
如果猜测==答案:
打印(“正确”)
其他:
打印(“不正确”)

跟踪当前操作是什么,并使用该值来比较

操作
仍然只是一个列表,因此您永远不会更新
answer=0
。添加一些要调试的
print
s。是否确实希望
Num1
Num2
对所有10个问题具有相同的值?如果没有,您需要在
while
循环中放入
Num1=random.randint(1,30)
Num2=random.randint(1,30)
。是的,我意识到在代码开始工作后,我已经更改了它,谢谢。