多选,easygui按钮框

Multi choice, easygui buttonbox

我们尝试使用 easygui、buttonbox 创建一个多选菜单。 我们已经厌倦了 if、elseelif 语句,但是无论何时选择其中一个数学主题选项,它总是带着玩家去addition/will问玩家加法问题。 我们不确定这是 if/else/elif 语句 的问题(我们希望 elif 可以工作) 还是 Figure1/Figure2 的另一个问题我们好像找不到。

这是我们的编码示例,我们只向您展示了主题选择菜单、加法和减法问题。 (希望为您节省所有时间,因为所有主题都是相同的,它们只是更改为 DivisionAnswers 和 MultiplicationAnswers 等。) 但我们关注的是内部问题主题选择

   Subject = easygui.buttonbox ("What maths subject would you like to test?", choices = ["Addition","Subtraction","Multiplication","Division"])
if "Addition":
    easygui.msgbox ("Please enter the correct answer to earn a point, there are 10 questions in this quiz")

for number in range(0,10):
    Figure1 = random.randrange(0,11)
    Figure2 = random.randrange(0,11)

    PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " + " +str(Figure2)+ "?")

    if int(PlayerAnswer) == Figure1 + Figure2:
        AdditionAnswers += 1
        easygui.msgbox ("Correct! Your score is "+str(AdditionAnswers))
        print(AdditionAnswers)#To see if game is keeping count of questions asked
    else:
        AdditionAnswers += 0
        IncorrectAnswers += 1
        easygui.msgbox ("Sorry, incorrect! Your score is still "+str(AdditionAnswers))
        print(AdditionAnswers)#To see if game is keeping count of questions asked

easygui.msgbox ("You scored " +str(AdditionAnswers)+ " out of 10")

print(AdditionAnswers) #To check if programme is calculating score correctly
print(IncorrectAnswers)

elif "Subtraction":
    easygui.msgbox ("Please enter the correct answer to earn a point, there are 10 questions in this quiz")

for number in range(0,10):
    Figure1 = random.randrange(0,11)
    Figure2 = random.randrange(0,11)

    PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " - " +str(Figure2)+ "?")

    if int(PlayerAnswer) == Figure1 - Figure2:
        SubtractionAnswers += 1
        easygui.msgbox ("Correct! Your score is "+str(SubtractionAnswers))
        print(SubtractionAnswers)#To see if game is keeping count of questions asked
    else:
        SubtractionAnswers+= 0
        IncorrectAnswers += 1
        easygui.msgbox ("Sorry, incorrect! Your score is still "+str(SubtractionAnswers))
        print(SubtractionAnswers)#To see if game is keeping count of questions asked

easygui.msgbox ("You scored " +str(SubtractionAnswers)+ " out of 10")

print(SubtractionAnswers) #To check if programme is calculating score correctly
print(IncorrectAnswers)

感谢您给我们的任何帮助或建议,非常感谢,因为我们都刚刚开始学习 python 编程。

编辑~ 它似乎绕过了所有其他科目并直接进入加法,无论选择什么科目

if/elifs好像没问题。比较是错误的。应该是:

if Subject == "Addition":

您毕竟是将按钮框中的选择存储在名为 Subject 的变量中
其余同理。祝你好运 ! PS。一般概念是用小写字母命名变量。大写字母以某种方式保留给 类。 编辑:为您修复了代码。大量缩进错误。

import easygui
import random
AdditionAnswers = 0
IncorrectAnswers = 0
SubtractionAnswers = 0
Subject = easygui.buttonbox ("What maths subject would you like to test?", choices = ["Addition","Subtraction","Multiplication","Division"])
if Subject == "Addition":
    easygui.msgbox ("Please enter the correct answer to earn a point,there are 10 questions in this quiz")
    for number in range(0,10):
        Figure1 = random.randrange(0,11)
        Figure2 = random.randrange(0,11)
        PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " + " +str(Figure2)+ "?")
        if int(PlayerAnswer) == Figure1 + Figure2:
            AdditionAnswers += 1
            easygui.msgbox ("Correct! Your score is "+str(AdditionAnswers))
            print(AdditionAnswers)#To see if game is keeping count of questions asked
        else:
            AdditionAnswers += 0
            IncorrectAnswers += 1
            easygui.msgbox ("Sorry, incorrect! Your score is still "+str(AdditionAnswers))
        print(AdditionAnswers)#To see if game is keeping count of questions asked

    easygui.msgbox ("You scored " +str(AdditionAnswers)+ " out of 10")
    print(AdditionAnswers) #To check if programme is calculating score correctly
    print(IncorrectAnswers)

elif Subject == "Subtraction":
    easygui.msgbox ("Please enter the correct answer to earn a point, there are 10 questions in this quiz")
    for number in range(0,10):
        Figure1 = random.randrange(0,11)
        Figure2 = random.randrange(0,11)
        PlayerAnswer = easygui.enterbox ("What is " +str(Figure1)+ " - " +str(Figure2)+ "?")

        if int(PlayerAnswer) == Figure1 - Figure2:
            SubtractionAnswers += 1
            easygui.msgbox ("Correct! Your score is "+str(SubtractionAnswers))
            print(SubtractionAnswers)#To see if game is keeping count of questions asked
        else:
            SubtractionAnswers+= 0
            IncorrectAnswers += 1
            easygui.msgbox ("Sorry, incorrect! Your score is still "+str(SubtractionAnswers))
            print(SubtractionAnswers)#To see if game is keeping count of questions asked

    easygui.msgbox ("You scored " +str(SubtractionAnswers)+ " out of 10")
    print(SubtractionAnswers) #To check if programme is calculating score correctly
    print(IncorrectAnswers)