簡體   English   中英

如何循環回到開頭

[英]How to loop back to beginning

我將如何解決這個問題:

print 'Adventure Game'

choice_1 = raw_input('You are travelling down a long road, there is a fork in the route, one side goes to a jungle, and the other leads to a hill biome, which side do you choose?            J for Jungle, H for Hill ')

if choice_1 == 'J':
    print 'Jungle?, very well then' 

elif choice_1 == 'H':
    print 'Hill, good decision'

if choice_1 == 'J':
    choice_2 = raw_input('In the jungle, a king cobra appears behind you, as you look forward, an ape leaps from a tree onto the ground. Do you take your chances with the venomous king cobra, or the enormous ape? C for cobra, A for ape ')

while choice_2 != 'A':
    print 'Sorry, you were bit by the cobra and died'
    print 'Please try again'

if choice_2 == 'A':
    break

elif choice_2 == 'A':
    print 'You were almost mauled by the ape, luckily, it fleed the scene after loosing sight of you'

如果用戶為choice_2選擇c,我希望它重新啟動,這樣每次發生這種情況時它都會從頭開始

我建議為每個位置使用函數,這樣您就可以多次調用相同的 function 或在其他函數中調用。

例如:

def road():
    choice = ''
    while choice not in ['j','h']:
        choice = raw_input('You are travelling down a long road, there is a fork in the route, one side goes to a jungle, and the other leads to a hill biome, which side do you choose?            J for Jungle, H for Hill ').lower()
    if choice == 'j':
        jungle()
    elif choice == 'h':
        hill()

def jungle():
    choice = ''
    while choice not in ['c','a']:
        choice = raw_input('In the jungle, a king cobra appears behind you, as you look forward, an ape leaps from a tree onto the ground. Do you take your chances with the venomous king cobra, or the enormous ape? C for cobra, A for ape ').lower()

    if choice == 'c':
        print 'Sorry, you were bit by the cobra and died'
        print 'Please try again'
        return
    elif choice == 'a':
        print 'You were almost mauled by the ape, luckily, it fleed the scene after loosing sight of you'
        next_func_here()

while True:
    print 'Adventure Game Starts Here!'
    road()
    print 'restarting...'

這樣您就可以快速輕松地繼續添加功能,並且也更易於調試。 while循環意味着如果road()退出,它會循環回到開頭並重新啟動road()

print 'Adventure Game'
choice_1 = raw_input('You are travelling down a long road, there is a fork in the route, one side goes to a jungle, and the other leads to a hill biome, which side do you choose?            J for Jungle, H for Hill ')
if choice_c == 'J':
    while True:
        choice_2 = raw_input('In the jungle, a king cobra appears behind you, as you look forward, an ape leaps from a tree onto the ground. Do you take your chances with the venomous king cobra, or the enormous ape? C for cobra, A for ape ')
        if choice_2 != 'A':
            print 'Sorry, you were bit by the cobra and died'
            print 'Please try again'
        elif choice_2 == 'A':
            print 'You were almost mauled by the ape, luckily, it fleed the scene after loosing sight of you'
            break
elif choice_1 == 'H':
    print 'Hill, good decision'

您可以在底部輸入“您要繼續嗎?” 並將其設置為變量。 然后將整個游戲包裝在一個while循環中。

cont = True
while continue == True:
    #put all your code here
    cont = input("Would you like to continue? (T/F)")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM