2017-05-05 99 views
0

我希望標題足夠說明了。基本上,用戶輸入一個數字,數字必須在1到147之間。如果是,則達到下一個必需的輸入(num_guess)。但是如果數字不在參數範圍內,我們需要讓用戶再次嘗試。但我無法弄清楚如何做到這一點。提前致謝。如果返回else語句,如何返回到循環的頂部

word_correct = False 

def length_check(x): 
    while (word_correct == False): 
     if x >= 1 and x <= 147: 
      return word_correct == True 
      break 
     else: 
      print("Try another number") 
      # print(input("Please enter a word length: ")) ## ignore me 

word_length = input("Please enter a word length: ") 
length_check(word_length) 

num_guess = raw_input("Please enter an amount of guesses: ") 
+1

這沒有意義....你究竟在做什麼? –

回答

0

基本上你不需要休息後return.Once編譯器遇到返回,它不經過下一個語句。 你的代碼有什麼錯誤?

  • 執行不正確。
  • 利用假期或者返回不期望的地方

滿足您需求的代碼可以實現如下 -

def length_check(x): 
    while(1): 
      temp = input() 
      if temp >= 1 and temp <= 147: 
       return 
      else: 
       print 'Enter proper length' 

這應該解決您的問題。

+0

但如何我提示用戶重新輸入另一號碼和運行while循環再次結束? – MLJezus

+0

@Tyson,這部分是通過此代碼中的'else'完成的。如果條件不滿意,它將進入'else',打印該消息,然後在循環中進行迭代,該循環將再次將用戶輸入置於'temp'中 – kuro

+0

這由'else'部分處理。如果輸入不在範圍內,它會打印出'輸入正確的長度'並且它會循環。while循環中的條件使它循環一次又一次。 – bigbounty

1

試試這個

word_correct = False 

def length_check(x): 
    while (word_correct == False): 
     if x >= 1 and x <= 147: 
      return word_correct == True 
     else: 
      print("Try another number") 
      new_word= input("Please enter a word length: ") 
      length_check(new_word) 

word_length = input("Please enter a word length: ") 
length_check(word_length) 

num_guess = raw_input("Please enter an amount of guesses: ") 
+0

這被卡在else語句中。如果在這些參數之外輸入一個數字,然後在其中輸入一個數字,則會生成以下輸出。 「 請輸入字長:150 嘗試另一個號碼 請輸入字長:10 嘗試另一個號碼 請輸入字長: 」 – MLJezus

0

這種策略是更簡潔,並採用遞歸。需要什麼你正在嘗試做無while循環:

def main(warning=''): 
    if warning: 
    print(warning) 
    word_length = input("Please enter a word length: ") 
    if word_length >= 1 and word_length <= 147: 
    num_guess = raw_input("Please enter an amount of guesses: ") 
    else: 
    main(warning="Try another number") 
    #... now do something with num_guess 
1

我不會試圖把word_check內的循環。將檢查長度與控制流程&打印消息的責任分開。

您可以使用無限循環,該循環只有在輸入有效值時纔會打破。

另外不要忘記轉換爲int。我假設你正在使用python 3?用戶可能輸入非有效整數的內容(例如​​),因此請使用異常來處理該內容。如果您使用的是Python 2,請將input替換爲raw_input

def length_check(x): 
    return x >= 1 and x <= 147 

while True: 
    try: 
     word_length = int(input("Please enter a word length: ")) 
    except ValueError: 
     print('please enter a valid number') 
     continue 

    if(length_check(word_length)): 
     break 

    print("Try another number") 

請注意,此方法只涉及input的單個實例。這會讓以後的生活更輕鬆。例如,如果您想更改消息並且不必記住要在兩個位置更改它,或者將它分配給某個字符串變量。

0

這可能會實現:

else: 
    word_length = input("Try another number") 
    length_check(word_length) 

在一個錯誤的猜測的事件(else)你問用戶再輸入word_length,然後再次運行檢查功能length_check。用戶可以根據自己的喜好多次查看(即通過輸入錯誤值繼續採取else分支),直到他們得到正確的號碼並滿足if條件。

如果您使用此功能,您可能無需使用break即可離開。

0

你可以試試這個

word_correct = False 

def length_check(x): 
    while (word_correct == False): 
     if x >= 1 and x <= 147: 
      word_correct == True 
      break 
     else: 
      x = input("Try another number") 
      # print(input("Please enter a word length: ")) ## ignore me 
    return 
word_length = input("Please enter a word length: ") 
length_check(word_length) 
0

一個while True with break塊的作品就像一個do{} until()在其他語言,所以下面的程序,以滿足問題的陳述。另一點是要注意遞歸限制,代碼中沒有硬性限制。

hint = "Please enter a word length: " 
while True: 
    x = int(raw_input(hint).strip()) 
    if 1 <= x <= 147: 
     break 
    else: 
     hint = "Try another number" 
num_guess = int(raw_input("Please enter an amount of guesses: ").strip())