如何在Python中中断特定的while循环?

时间:2019-03-22 08:13:30

标签: python python-3.x while-loop

我正在为学校CMP课程构建一个程序,该程序执行游戏,用户应根据给出的线索(例如口头游戏)和二十个问题来猜测和匹配国家/地区名称。我的总数是5条线索,而不是原始游戏的20条线索。为此游戏,我为国家/地区设置了20个选项。

我的程序很长,因此我将跳过所有其他19个国家/地区,仅以一个国家/地区为例。所以,我的问题是我不知道如何在下面的这种情况下打破while循环。我使用了while循环让用户继续在其他国家/地区继续游戏问题,但我也想让用户决定他们将继续还是结束游戏。当他们决定完成程序时,我想停止整个程序,但这不起作用。

#Setting
n = 0
i = 0
pt = 0
opt = 1

#Game start
while i <= 20:
    while n == 0:

#Countries list
        if opt == 1:
            option = 'germany'
            n = 1

#Display # of letters
    while n == 1:
        letter_num = len(option)
        if letter_num == 5:
            print("This country is 5 letters! Lucky! '.'\n ")
        elif letter_num == 6:
            print("This country is 6 letters! Good luck! '_'\n ")
        elif letter_num == 7:
            print("This country is 7 letters! Unlucky! :(\n ")
        elif letter_num == 8:
            print("This country is 8 letters! It's challenging! :(\n ")
        n = 2

#Answering
    while n == 2:
        if opt == 1:
            print(wrong_0)
            ans = str(input("1st Clue: This country is in Europe.\nWhat country is this?: "))
            if ans == 'germany' or ans == 'Germany':
                print("\nPerfect! You are correct!")
                pt += 10
            else:
                print(wrong_1)
                ans = str(input("Wrong...\n2nd Clue: This country is a memeber of G7.\nWhat country is this?: "))
                if ans == 'germany' or ans == 'Germany':
                    print("\Amazing! You are correct!")
                    pt += 8
                else:
                    print(wrong_2)
                    ans = str(input("Wrong...\n3rd Clue: This country has black, red, and gold color on its flag.\nWhat country is this?: "))
                    if ans == 'germany' or ans == 'Germany':
                        print("\nGreat! You are correct!")
                        pt += 5
                    else:
                        print(wrong_3)
                        ans = str(input("Wrong...\n4th Clue: This country has a captial city named Berlin.\nWhat country is this?: "))
                        if ans == 'germany' or ans == 'Germany':
                            print("\nGood job! You are correct!")
                            pt += 3
                        else:
                            print(wrong_4)
                            ans = str(input("Wrong...\n5th Clue: This country starts with letter g.\nWhat country is this?: "))
                            if ans == 'germany' or ans == 'Germany':
                                print("\nYes! You are correct!")
                                pt += 1
                            else:
                                print(fail)
                                pt += 0
                                print("Answer was 'GERMANY'!.. Try again..")

# Printing the result(score) & Start the new game
        print("\n(★≧▽^))★☆Your cumulative points:", pt,"pts.")
        print("-"*80)
        choice = str(input("Will you continue the game?(yes/no): "))
        if choice == 'yes':
            n = 0
            i += 1
            opt += 1
            print("-"*80)
        if choice == 'no':
            print("Thank you for playing this game! :D\nTHE END...")
            print("-"*80)
            break

0 个答案:

没有答案