簡體   English   中英

我在做什么錯,我不確定如何修復我的代碼

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM