gpt4 book ai didi

Python程序不循环/重新启动

转载 作者:行者123 更新时间:2023-12-01 04:57:38 26 4
gpt4 key购买 nike

我有一个学校评估,是为了制作一个 child 的拼写游戏,当玩家单击"is"时,它必须循​​环/重新启动。到目前为止,当我测试游戏时,询问玩家是否想再次玩的选项/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 很陌生,所以我能得到的任何帮助都会很棒。我知道我之前曾就这段代码寻求过帮助,但我今天才刚刚注册了一个帐户,所以我不知道如何回复任何评论或任何东西!

最佳答案

要在用户回答"is"时再次玩游戏,您需要将游戏置于 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

注释:

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

  • 总结用户表现的代码被减少到几行,并从字典中获取鼓励的话。

关于Python程序不循环/重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27009710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com