【问题标题】:Multi choice, easygui buttonbox多选,easygui按钮框
【发布时间】:2015-11-26 19:32:17
【问题描述】:

我们尝试使用 easygui、按钮框创建一个多选菜单。 We have tired if, else and elif statements but whenever selecting one of the math subject options, it always takes the player to addition/will ask玩家加法问题。 我们不确定是否是 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 编程。

编辑~ 它似乎绕过了所有其他主题并直接添加,无论选择什么主题

【问题讨论】:

    标签: python if-statement random easygui


    【解决方案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)
    

    【讨论】:

    • 我为所有的人都这样做了,但是当我们点击其他主题时它仍然会询问我们添加的问题 edit 它似乎绕过了所有其他主题并直接进入添加不管选择什么科目
    • 刚刚编辑了我的答案。你的压痕到处都是
    • 非常感谢您! (听起来像我,我太懒了,或者完全忘记了一半时间正确缩进!)非常感谢您的帮助,这意味着很多!
    • 考虑使用像 PyCharm 这样的 IDE。它会在您键入时显示语法错误。祝你好运。
    • 现在很好用!非常感谢达米安!
    猜你喜欢
    • 2021-05-19
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多