Python程序没有循环/重启

时间:2014-11-19 05:35:22

标签: python loops easygui

我有一个学校评估,即制作一个孩子的拼写游戏,当玩家点击是时,它必须循​​环/重启。到目前为止,当我测试游戏时,选项/ easygui.buttonbox会询问玩家是否想要再次玩游戏以及是/否选项再次玩或退出游戏时不会出现。该节目在播放器最终得分显示后立即关闭。

这是我对游戏的编码,我找不到我做错了什么,我已经尝试了3个建议来修复编码,但没有一个有效。

import easygui
#Childs Litercay Game
#Charlotte Lowe 03/09/2015

#Declare Constants and Variables
Score = 0
PlayerAnswer = 0
playOn = 0
while playOn != "Yes":
    playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
if "Yes":
    PlayerName = easygui.enterbox ("Before we begin, what's your name?")

easygui.msgbox ("Hi " + PlayerName +"! Here are the rules of the game.")
easygui.msgbox ("There will be 3 words you can choose from, 2 will be incorrect and 1 will be right.")
easygui.msgbox ("All you have to do is select the correctly spelt word.")
easygui.msgbox ("For every right word you pick you will earn a point! There are 10 questions in this quiz")
easygui.msgbox ("Let's begin!")

PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices = ["Awesome","Awsome","Awesom"])
if PlayerAnswer == "Awesome":
    Score += 1
    easygui.msgbox ("You're right! Your score is " + str(Score))
elif PlayerAnswer == "Awsome":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is " + str(Score))
elif PlayerAnswer == "Awesom":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Becuse","Becus","Because"])
if PlayerAnswer == "Becuse":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Becus":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Because":
    Score += 1
    easygui.msgbox ("Correct! Your score is now " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Morning","Moring","Morening"])
if PlayerAnswer == "Morning":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Moring":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Morening":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Beleave","Believe","Belive"])
if PlayerAnswer == "Believe":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Beleave":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Belive":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Jewelry","Jewlery","Jewley"])
if PlayerAnswer == "Jewelry":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Jewlery":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Jewley":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Mispelled","Misspelled","Misspeled",])
if PlayerAnswer == "Misspelled":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "Mispelled":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "Misspeled":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly    
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["acceptable","aceptable","acceptble",])
if PlayerAnswer == "acceptable":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "aceptable":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "acceptble":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["calendar","calender","callendar",])
if PlayerAnswer == "calendar":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "calender":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "callendar":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["equepment","equiptment","equipment",])
if PlayerAnswer == "equipment":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "equiptment":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "equipment":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["library","liebary","libary",])
if PlayerAnswer == "library":
    Score += 1
    easygui.msgbox ("Well done! Your score is now " + str(Score))
elif PlayerAnswer == "liebary":
     Score += 0
     easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
elif PlayerAnswer == "lieberry":
    Score += 0
    easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
print(Score) #To check if programme is calculating score properly
if Score == 10:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, you got them all right " + PlayerName+ "!")
elif Score == 9:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, one away " + PlayerName+ "!")
elif Score == 8:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, so close "+ PlayerName+"!")
elif Score == 7:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, nearly there " + PlayerName+"!")
elif Score == 6:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, amlost made it " + PlayerName+ "!")
elif Score == 5:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, half way there " + PlayerName+ "!")
elif Score == 4:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, a little more pratice " + PlayerName+ "!")
elif Score == 3:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, pratice makes perfect "+ PlayerName+"!")
elif Score == 2:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, try again " + PlayerName+"!")
elif Score == 1:
    easygui.msgbox ("You got a score of " + str(Score) + " out of 10, better luck next time " + PlayerName+ "!")
elif Score == 0:
    easygui.msgbox ("You got a score of " + str(Score) + "out of 10, better luck next time " + PlayerName+ "!")

while playOn != "Yes":
   playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
   if playOn == "Yes":
      Score = 0 #resets score count, if player wants to play again

   elif playOn == "No":
     easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")

我希望万维网上有人可以了解我做错了什么。我对python很新,所以我能得到的任何帮助都会非常棒。我知道我之前已经请求过这段编码的帮助,但我今天才刚开帐,所以我不知道如何回复任何评论或任何内容!

2 个答案:

答案 0 :(得分:1)

您在第一个循环中将playOn设置为"Yes"

while playOn != "Yes":
    playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
if "Yes":
    PlayerName = easygui.enterbox ("Before we begin, what's your name?")

在程序结束时,您只会在while不等于playOn时进入"Yes"循环。

while playOn != "Yes":
   playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
   if playOn == "Yes":
      Score = 0 #resets score count, if player wants to play again

   elif playOn == "No":
     easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")

但是,你在程序开头设置(并且没有更改)playOn"Yes",所以跳过循环并且程序结束。


一些帮助你的笔记

  • “amlost make it”/“更多pratice”/“pratice完美” - 你的拼写检查程序中有一些拼写错误。
  • 程序非常冗长,你有很多冗余代码。你提到这是一个学校作业,所以我不打算提供更短的代码样本,但我鼓励你查找功能。
  • 即使使用playOn解决主要问题,点击“是”也不会重置您的程序。你仍然需要一种方法来到你的拼写检查代码的开头(提示:函数再次是你的朋友/或者外部循环也会有帮助)

答案 1 :(得分:1)

如果用户回答是,要让它再次播放,您需要将游戏置于while循环中。只需对游戏进行最小程度的更改,就可能看起来像:

import easygui
#Childs Litercay Game
#Charlotte Lowe 03/09/2015

#Declare Constants and Variables
Score = 0
PlayerAnswer = 0
playOn = 0
while playOn != "Yes":
    playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
if "Yes":
    PlayerName = easygui.enterbox ("Before we begin, what's your name?")

easygui.msgbox ("Hi " + PlayerName +"! Here are the rules of the game.")
easygui.msgbox ("There will be 3 words you can choose from, 2 will be incorrect and 1 will be right.")
easygui.msgbox ("All you have to do is select the correctly spelt word.")
easygui.msgbox ("For every right word you pick you will earn a point! There are 10 questions in this quiz")
easygui.msgbox ("Let's begin!")

while True:
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices = ["Awesome","Awsome","Awesom"])
    if PlayerAnswer == "Awesome":
        Score += 1
        easygui.msgbox ("You're right! Your score is " + str(Score))
    elif PlayerAnswer == "Awsome":
            Score += 0
            easygui.msgbox ("Sorry Incorrect! Your score is " + str(Score))
    elif PlayerAnswer == "Awesom":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Becuse","Becus","Because"])
    if PlayerAnswer == "Becuse":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Becus":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Because":
        Score += 1
        easygui.msgbox ("Correct! Your score is now " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Morning","Moring","Morening"])
    if PlayerAnswer == "Morning":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Moring":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Morening":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Beleave","Believe","Belive"])
    if PlayerAnswer == "Believe":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Beleave":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Belive":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Jewelry","Jewlery","Jewley"])
    if PlayerAnswer == "Jewelry":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Jewlery":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Jewley":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["Mispelled","Misspelled","Misspeled",])
    if PlayerAnswer == "Misspelled":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "Mispelled":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "Misspeled":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly____
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["acceptable","aceptable","acceptble",])
    if PlayerAnswer == "acceptable":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "aceptable":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "acceptble":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["calendar","calender","callendar",])
    if PlayerAnswer == "calendar":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "calender":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "callendar":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["equepment","equiptment","equipment",])
    if PlayerAnswer == "equipment":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "equiptment":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "equipment":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices =["library","liebary","libary",])
    if PlayerAnswer == "library":
        Score += 1
        easygui.msgbox ("Well done! Your score is now " + str(Score))
    elif PlayerAnswer == "liebary":
         Score += 0
         easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    elif PlayerAnswer == "lieberry":
        Score += 0
        easygui.msgbox ("Sorry Incorrect! Your score is still " + str(Score))
    print(Score) #To check if programme is calculating score properly
    if Score == 10:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, you got them all right " + PlayerName+ "!")
    elif Score == 9:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, one away " + PlayerName+ "!")
    elif Score == 8:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, so close "+ PlayerName+"!")
    elif Score == 7:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, nearly there " + PlayerName+"!")
    elif Score == 6:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, amlost made it " + PlayerName+ "!")
    elif Score == 5:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, half way there " + PlayerName+ "!")
    elif Score == 4:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, a little more pratice " + PlayerName+ "!")
    elif Score == 3:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, pratice makes perfect "+ PlayerName+"!")
    elif Score == 2:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, try again " + PlayerName+"!")
    elif Score == 1:
        easygui.msgbox ("You got a score of " + str(Score) + " out of 10, better luck next time " + PlayerName+ "!")
    elif Score == 0:
        easygui.msgbox ("You got a score of " + str(Score) + "out of 10, better luck next time " + PlayerName+ "!")

    playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
    if playOn == "Yes":
       Score = 0 #resets score count, if player wants to play again
    elif playOn == "No":
      easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")
      break

更简单的表格

计算机在重复性方面比人类更好。修改以下代码以利用:

import easygui
#Childs Litercay Game
#Charlotte Lowe 03/09/2015

def play(choices, answer, Score):
    """Check spelling."""
    PlayerAnswer = easygui.buttonbox ("Which word is spelt correctly?", choices=choices)
    if PlayerAnswer == choices[answer]:
        Score += 1
        easygui.msgbox ("You're right! Your score is " + str(Score))
    else:
        easygui.msgbox ("Sorry Incorrect! Your score is " + str(Score))
    return Score
#    print(Score) #To check if programme is calculating score properly

tests = (
        (["Awesome","Awsome","Awesom"], 0),
        (["Becuse","Becus","Because"], 2),
        (["Morning","Moring","Morening"], 0),
        (["Beleave","Believe","Belive"], 1),
        (["Jewelry","Jewlery","Jewley"], 0),
        # Fill in the rest.....
    )

congrats = {
        10:"you got them all right ",
        9:"one away",
        8:"so close",
        7:"nearly there",
        6:"amlost made it",
        5:"half way there",
        4:"a little more pratice",
        3:"pratice makes perfect",
        2:"try again",
        1:"better luck next time",
        0:"better luck next time",
}

# Hello and instructions
playOn = easygui.buttonbox ("Hey there, are you ready to test your spelling?", choices = ["Yes"])
PlayerName = easygui.enterbox ("Before we begin, what's your name?")
easygui.msgbox ("Hi " + PlayerName +"! Here are the rules of the game.")
easygui.msgbox ("There will be 3 words you can choose from, 2 will be incorrect and 1 will be right.")
easygui.msgbox ("All you have to do is select the correctly spelt word.")
easygui.msgbox ("For every right word you pick you will earn a point! There are 10 questions in this quiz")
easygui.msgbox ("Let's begin!")

while True:
    Score = 0
    for choices, answer in tests:
        Score = play(choices, answer, Score)
        print(Score) #To check if programme is calculating score properly
    easygui.msgbox (congrats[Score] +" " + PlayerName+ "!")

    playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
    if playOn == "No":
      easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")
      break

注意:

  • 现在,在为每组选择多次调用的函数中,为用户提供三种选择并更新分数的代码会出现一次。

  • 将用户表现总结的代码减少到几行,从字典中得到鼓励的话语。