2016-08-14 232 views
1

我從一本書中發明了這個猜測遊戲的想法,Invent With Python。我不喜歡原始腳本沒有涵蓋重新猜測數字或錯誤地使用不在1-20中的數字的可能性,所以我修改了它。該程序很好用,但是,如果/ elif/else代碼塊只是我的頭部。如果聲明,避免嵌套? (Python 2.7)

我想重寫腳本,而不必嵌套,如果在if中。我甚至不知道如何做到這一點。任何人都可以請幫助我 - 只有一個例子說明這個程序如何在沒有嵌套的情況下工作會很棒!

下面是完整的小腳本:

from random import randint 
from sys import exit 
name = raw_input("Hello! What's your name? ") 
print "Well %s, I'm thinking of a number between 1 and 20." % name 
print "Since I'm a benevolent computer program, I'll give you 6 guesses." 
secret_number = randint(1, 20) 

guesses_left = 6 
already_guessed = [] 

while guesses_left > 0: 
    try: 
     guess = int(raw_input("Take a guess: ")) 

     if guess >= 1 and guess <= 20 and guess not in already_guessed: 
      already_guessed.append(guess) 
      guesses_left -= 1 

      if guess == secret_number: 
       print "You win! %d was my secret number!" % secret_number 
       exit(0) 
      elif guess < secret_number: 
       print "Your guess is too low!" 
      elif guess > secret_number: 
       print "Your guess is too high!" 

     elif guess in already_guessed: 
      print "You already guessed that!" 

     else: 
      print "Not a number between 1 - 20!" 
      print "Please try again!" 

     print "You have %d guesses left!" % guesses_left 

    except ValueError: 
     print "Invalid input! Please try again!"  
+1

您可以使用[的'繼續'語句](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops),這也將有助於除了輸入從'try'。 – Ryan

回答

5

試試這樣,使用continue退出循環的當前迭代,並在循環的頂部重新開始。

你也有一個邏輯錯誤的位置:

if guess <= 1 and guess >= 20 and guess not in already_guessed: 

許多不可能既小於或等於1,和大於或等於20你and應該是一個or是這樣的:

if (guess <= 1 or guess >= 20) and guess not in already_guessed: 

或者簡單:

if 1 <= guess <= 20 and guess not in already_guessed: 

另外,請ÿ我們try/except只左右,實際上可以產生一個異常(或不應該發生,如果發生異常的事情:

from random import randint 
import sys 

name = raw_input("Hello! What's your name? ") 
print "Well {}, I'm thinking of a number between 1 and 20.".format(name) 
print "Since I'm a benevolent computer program, I'll give you 6 guesses." 

secret_number = randint(1, 20) 
guesses_left = 6 
already_guessed = [] 

while guesses_left > 0: 
    print "You have {} guesses left!".format(guesses_left) 

    try: 
     guess = int(raw_input("Take a guess: ")) 
    except ValueError: 
     print "Invalid input! Please try again!\n" 
     continue 

    # If the number is not between 1 and 20... 
    if not (1 <= guess <= 20): 
     print "Not a number between 1 - 20!" 
     print "Please try again!\n" 
     continue 

    if guess in already_guessed: 
     print "You already guessed that!\n" 
     continue 

    guesses_left -= 1 
    already_guessed.append(guess) 

    if guess == secret_number: 
     print "You win! {} was my secret number!".format(secret_number) 
     sys.exit(0) 
    elif guess < secret_number: 
     print "Your guess is too low!\n" 
    elif guess > secret_number: 
     print "Your guess is too high!\n" 

下面是一個例子來看:

Hello! What's your name? :) 
Well :), I'm thinking of a number between 1 and 20. 
Since I'm a benevolent computer program, I'll give you 6 guesses. 
You have 6 guesses left! 
Take a guess: 2 
Your guess is too low! 

You have 5 guesses left! 
Take a guess: 2 
You already guessed that! 

You have 5 guesses left! 
Take a guess: 3 
Your guess is too low! 

You have 4 guesses left! 
Take a guess: 7 
Your guess is too high! 

You have 3 guesses left! 
Take a guess: 5 
Your guess is too high! 

You have 2 guesses left! 
Take a guess: 4 
You win! 4 was my secret number! 
+1

我喜歡這個最好的,謝謝!我特別喜歡try/except塊上的指針 - 我一定誤解了關於try/except塊的在線Python文檔 - 它看起來像是需要嵌套在其中的所有東西。 –

+0

沒問題,很高興幫助:)是的,想想'try:... except:...'as'在這個塊中運行代碼,並且如果這個塊內的任何代碼失敗,立即跳轉到代碼在'except:'並運行它「。因此,由於'except:'(在我上面的示例中)執行了「continue」,循環中的其餘代碼將永遠不會執行。循環其餘部分的代碼永遠不會引發一個'ValueError'。有時候,你確實想要放置不會在'try:'塊內產生錯誤的代碼,例如,如果你不能'繼續','break'或'return',並且你不想要代碼繼續執行。 – Will

+1

此外,如果這解決了您的問題,請單擊答案旁邊的複選標記以將問題標記爲已解決:)謝謝! – Will

1

只要改變嵌套的if語句elif的,像這樣:

from random import randint 
from sys import exit 
name = raw_input("Hello! What's your name? ") 
print "Well %s, I'm thinking of a number between 1 and 20." % name 
print "Since I'm a benevolent computer program, I'll give you 6 guesses." 
secret_number = randint(1, 20) 

guesses_left = 6 
already_guessed = [] 

while guesses_left > 0: 
    try: 
     guess = int(raw_input("Take a guess: ")) 

     if guess <= 1 and guess >= 20 and guess not in already_guessed: 
      already_guessed.append(guess) 
      guesses_left -= 1 

     elif guess == secret_number: 
      print "You win! %d was my secret number!" % secret_number 
      exit(0) 
     elif guess < secret_number: 
      print "Your guess is too low!" 
     elif guess > secret_number: 
      print "Your guess is too high!" 

     elif guess in already_guessed: 
      print "You already guessed that!" 

     else: 
      print "Not a number between 1 - 20!" 
      print "Please try again!" 

     print "You have %d guesses left!" % guesses_left 

    except ValueError: 
     print "Invalid input! Please try again!" 

這將是我看到解決您的兩難困境最簡單的方法

+1

這從根本上改變了程序的控制流程。 – Will