python - 游戏循环基础知识
问题描述
我正在做这个练习任务,这是一个测验,场景是如果问题的答案是错误的。玩家可以尝试再试一次,但是一旦我输入了正确的答案,它会再次运行问题而不是下一个. 它一直在重复这个问题。
知道如何保留相同的问题,如果它是错误的并且一旦更正它将移至下一个问题,如果玩家在给定的重试中没有正确回答它,它将移至下一个问题?
这是我的代码:
print("USE FUNCTION --> game()")
def main():
pass
qs = ["Grass", "Horn", "Feather", "Laugh", "Smooth", "Mountain", "Abundance", "absorb", "cheapskate", "citizenship", "classify", "kingdom", "kilometer", "poet", "free"]
an = ["damo", "sungay", "balihibo", "tawa", "makinis", "bundok", "kasaganahan", "sipsipin", "barat", "pagkamamamayan", "suriin", "kaharian", "kilometro", "makata", "malaya"]
##def attempt():
## count = 0
## att = input("Ilagay ang iyong sagot: ")
##
## print("Mali ang iyong sagot, Subukan Muli")
## break
def game():
print("---------------------------------")
print("Welcome to game DAY 2 Quiz - Translate English to Tagalog")
print("---------------------------------\n")
score = 0
count = 0
#Select I then use range staring from 0 till the end minus
while count < 3:
for i in range (0,3):
student = input(qs[i]+ "\n Ilagay ang iyong sagot: ").upper()
## while count < 3:
if student == an[i].upper():
print("Correct\n")
score += 1
else:
print("\n Wrong! Try Again\n")
print("Attempt",count)
count += 1
break
print("Ang tamang sagot ay",an[i])
name = input("What is you name: ").upper()
print("Hey!",name,"Your final score is",score,"out of 15\n")
input("Press ENTER to exit")
谢谢
解决方案
反转for循环和while循环
看起来您正在使用“while count < 3”处理重试计数
和“for i in range (0,3)”的问题
#if you want to go through all questions, try len(qs)
for i in range (0,3):
#reset your retry count?
count = 0
while count < 3:
student = input(qs[i]+ "\n Ilagay ang iyong sagot: ").upper()
## while count < 3:
if student == an[i].upper():
print("Correct\n")
score += 1
break #<-instead, break here
else:
print("\n Wrong! Try Again\n")
print("Attempt",count)
count += 1
#break <-no need to break here