游戏循环基础

时间:2018-11-19 01:05:00

标签: python python-3.x

我正在做这个练习题,这是一个测验,方案是答案是否正确。玩家可以尝试再次尝试,但是一旦我输入正确的答案,它将再次运行该问题,而不是下一个。整个问题都在重复。

有什么主意,如果错误,我将如何保留相同的问题,一旦更正,它将移至下一个问题;如果玩家在给定的重试中未正确回答该问题,它将移至下一个问题?

这里是我的代码:

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")

谢谢

2 个答案:

答案 0 :(得分:0)

反转for循环和while循环

看起来您正在使用“ while count <3”来处理重试计数

以及“ for i(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

答案 1 :(得分:0)

您可以玩循环游戏吗? 试试这个:

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"]

i, score = 0, 0
main_bucle = True

while main_bucle:

    count = 0

    if i == 15:
        break
    else:
        i+=1

    print(qs[i-1]+ "\nIlagay ang iyong sagot: ")

    while count < 3:

        print("Attempt: " + str(count+1))

        answ = input().upper()

        if answ == an[i-1].upper():
            print("\nCorrect!\n")
            score += 1
            break

        else:
            print("\nWrong! Try Again\n")
            count+=1

print("You score is: " + str(score))