多选,easygui按钮

时间:2015-11-26 19:32:18

标签: python if-statement random easygui

我们尝试使用easygui,buttonbox创建一个多选菜单。 我们已经厌倦了 if,else elif 语句但是每当选择其中一个数学主题选项时,它总是需要玩家添加/将要求球员补充问题。 我们不确定if / else / elif语句是否存在问题(我们希望elif能够工作)或者如果它是图1 /图2中的另一个问题我们似乎无法找。

以下是我们编码的示例,我们只向您展示了主题选择菜单,添加和减法问题。 (希望能节省你所有的时间,因为所有的主题是相同的,它们只是被改为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编程。

编辑〜 它似乎绕过所有其他科目,无论选择什么科目,都直接加入

1 个答案:

答案 0 :(得分:1)

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)