Python编程 - 退出循环并显示分数

时间:2013-10-05 11:25:08

标签: python

我的正在进行的“项目”确实需要一些帮助,因为我是一个新手 - 不仅仅是在Python中,而是编程本身 - 已经使用Python一周了。

我正在尝试创建的是一个小小的“测验”,菜单工作正常,这是最简单的创建,在菜单中选择nr 2,退出测验,但我有一些问题其余的代码。我需要修复的是,每次玩家回答一个问题后,如果他/她想要继续玩,他会被提示一个问题,如果是(“ja”),玩家会得到一个新的随机问题,如果不是当前的分数(百分比)显示,程序关闭。随机问题工作正常,继续工作正常,但退出测验和分数仍然是一个问题,我无法做到正确,也许这只是一个简单的错误,但不知怎的,我只是找不到它。

提前谢谢!

PS。由于某些未知原因,孔代码无法在代码框中正确显示,因此我只发布代码的“区域”问题。

while aqpool[0]:
    shuffle (aqpool)
    numRight = 0



    for question, rightAnswer in aqpool:
        answer = input(question + " ")

        playAgain = "ja"
        playStop = "nej"

        if answer == rightAnswer:
            print ("RÄTT SVAR!")

            numRight = numRight + 1


            if playAgain == "ja" or playAgain == "j":

                print("Vill du försätta spela? (ja eller nej)")
                playAgain = str(input())
                continue

            if playStop == "nej" or playStop == "n":
                total = numRight / len(aqpool) * 100
                print ("Du hade ",total, "% rätt!")
                playStop = str(input())
                break                                

        else:


            print("FEL SVAR! Rätta svaret är: " + rightAnswer + "\n")

            if playAgain == "ja" or playAgain == "j":
                print ("Vill du försätta spela? (ja eller nej)")
                playAgain = str(input())

            if playStop == "nej" or playStop == "n":
                total = numRight / len(aqpool) * 100
                print ("Du hade ", total, "% rätt!")
                playStop = str(input())
                break

1 个答案:

答案 0 :(得分:0)

userWantToContinue = True
while aqpool[0] and userWantToContinue:
    shuffle (aqpool)
    numRight = 0

    for question, rightAnswer in aqpool:
        answer = raw_input(question + " ")

        if answer == rightAnswer:
            print ("RÄTT SVAR!")

            numRight = numRight + 1

        else:
            print("FEL SVAR! Rätta svaret är: " + rightAnswer + "\n")


        print("Vill du försätta spela? (ja eller nej)")
        userWantToContinue = str(raw_input()) not in ('n', 'nej')
        if not userWantToContinue:
            break

    total = numRight / len(aqpool) * 100
    print ("Du hade %s%% rätt!" % total)