2013-04-18 157 views
4

我試圖做一個函數,其中有一個if/elif語句,並且我想要if打破一個while循環。該函數用於文本冒險遊戲,並且是/否問題。以下是我想出了迄今爲止..打破while循環功能?

def yn(x, f, g): 
    if (x) == 'y': 
     print (f) 
     break 
    elif (x) == 'n' 
     print (g) 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 

while True: 
    ready = raw_input('y/n ') 
    yn(ready, 'Good, let\'s start our adventure!', 
     'That is a real shame.. Maybe next time') 

現在我不知道如果我使用的功能權限,但是當我嘗試它,它說我不能有突破功能。所以如果有人可以幫助我解決這個問題,並且如果你可以幫助我,如果函數和調用函數本身的格式不對,那將非常感激。

+1

您的意思是讓函數內的行縮進嗎? – 2013-04-18 02:14:12

+0

我更新了我的解釋,如果您有任何其他問題,請隨時捅我。 – eandersson 2013-04-19 09:49:32

回答

0

一種方法是讓yn返回一個布爾值,然後用它來擺脫循環。否則,函數中的break不能跳出調用函數中的循環。

def yn(x, f, g): 
    if (x) == 'y': 
     print (f) 
     return True 
    elif (x) == 'n' 
     print (g) 
     return False 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 
done = False 
while not done: 
    ready = raw_input('y/n ') 
    done = yn(ready, 'Good, let\'s start our adventure!', 'That is a real shame.. Maybe next time') 
0

使用break,即使未滿足循環結束的條件,也可以退出循環。你不能休息,因爲'if/elif'不是一個循環,它只是一個條件語句。

1

您需要跳出循環內部的while循環,而不是從另一個函數中跳出。

像下面可能接近你想要什麼:

def yn(x, f, g): 
    if (x) == 'y': 
     print (f) 
     return 0 
    elif (x) == 'n': 
     print (g) 
     return 1 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 

while True: 
    ready = raw_input('y/n: ') 
    if (yn(ready, 'Good, let\'s start our adventure!', 'That is a real shame.. Maybe next time')): 
     break 
+0

你能解釋一下添加返回0和返回1是否有效嗎?我只是看着它而不太明白。 – 2013-04-18 02:32:33

+1

@Duane Moore您應該將其改爲'return True'並'return False'以提高可讀性。 – 2013-04-18 02:42:34

5

你可以用一個異常工作:

class AdventureDone(Exception): pass 

def yn(x, f, g): 
    if x == 'y': 
     print(f) 
    elif x == 'n': 
     print(g) 
     raise AdventureDone 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 

try: 
    while True: 
     ready = raw_input('y/n ') 
     yn(ready, "Good, let's start our adventure!", 
      'That is a real shame.. Maybe next time') 
except AdventureDone: 
    pass 
    # or print "Goodbye." if you want 

這一遍又一遍地循環的while循環,但裏面的yn()函數會產生一個打破循環的異常。爲了不打印回溯,必須捕獲並處理異常。

+0

創造性的答案,但在這種情況下打破while循環的唯一方法是按'n'。我假設他想在輸入'y'後繼續遊戲。 – eandersson 2013-04-19 14:16:15

1

您將需要將函數中的中斷更改爲返回,並且在用戶未向您提供正確輸入的情況下,您需要有一個else語句。最後,您需要將您的while loop中的電話轉爲if語句。

如果玩家輸入所需的命令,這將允許您打破while語句,如果沒有,它會再次提問。我還更新了您的yn函數,以允許用戶使用小寫字母和大寫字符,以及是和否。

def yn(input, yes, no): 
    input = input.lower() 
    if input == 'y' or input == 'yes': 
     print (yes) 
     return 1 
    elif input == 'n' or input == 'no': 
     print (no) 
     return 2 
    else: 
     return 0 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, %s. Are you ready for your adventure?' % name 

while True: 
    ready = raw_input('y/n ') 
    if yn(ready, 'Good, let\'s start our adventure!', 
     'That is a real shame.. Maybe next time') > 0: 
     break 

這背後的想法很簡單。 yn函數有三個狀態。用戶回答是,不是或無效。如果用戶的回答是「是」或「否」,則函數將返回1表示「是」,而2表示「否」。如果用戶不提供有效的輸入,例如空白空間,它會返回0

裏面的while True:循環中,我們包裹YN(「......‘’......」)函數與if statement來檢查,如果該yn函數返回一個數字大於0.因爲yn將返回0,如果用戶向我們提供了有效輸入,並且1或2爲有效輸入。

一旦我們從yn得到有效答覆,我們稱之爲休息,即停止while loop,我們就完成了。

+0

我沒有看到其他的巨大差異,已經7小時的答案,使'yn()'返回一個布爾類似的值... – glglgl 2013-04-18 09:47:45

+0

我告訴瑞恩,當我醒來時,我會給他一個答案。我遵守我的承諾,並且通過添加'else'語句並使其不區分大小寫來處理它。 http://stackoverflow.com/questions/16071394/cant-get-a-function-to-work/16071446#16071446 – eandersson 2013-04-18 09:53:35

+1

啊,'else'部分確實是非常好的東西。 – glglgl 2013-04-18 09:54:44

0
a = True 

def b(): 
    if input("") == "Quit": 
     global a 
     a == False 
    else: 
     pass 

while a == True: 
    print('Solution') 
+0

請解釋如何回答這個問題。 – jpaugh 2017-08-01 21:03:38

+0

如果在while循環中調用if語句,它將打破while循環。 – 2017-08-02 09:50:10