2017-05-23 24 views
1

我想創建一個學校項目的Python猜數字遊戲。我做了一個基本的遊戲,能夠很好地工作,但我想添加一些異常處理,以防用戶輸入錯誤。例如,這是我的代碼段:'<'不支持案件之間 - 猜數字遊戲

def Normal_Guess(): 
    number = round(random.uniform(0.0, 100.0),2) 
    guess = "" 
    while guess != number: 
     try: 
      guess = float(input("Please guess a number between 0 and 100: ")) 
     except ValueError or TypeError: 
      print ("That isn't even a number!") 

     if guess < number and guess >= 0: 
      print ("You need to guess higher!") 
     elif guess > number and guess <= 100: 
      print ("You need to guess lower!") 
     elif guess == number: 
      print("Congratulations! You guessed the number!") 
     elif guess < 0 or guess > 100: 
      print ("The number you guessed is not between 0 and 100") 
     else: 
      print("That isn't even a number!") 
    New_Game() 

也能正常工作,當用戶進入一個浮點或整數值爲「猜測」,並嘗試,除了條款我看來,如果用戶輸入捕捉首先,除了數字之外的任何事情,但該計劃似乎也繼續進行「如果」的陳述。我得到一個TypeError,說「'str'和'float'的實例之間不支持」'<'「。

我試圖包含一個Try-Except子句中的整個循環,並且不起作用。我不知道我做錯了什麼。任何幫助深表感謝。

+5

,用'繼續'在打印信息後。 – asongtoruin

+0

或者,在'try'-'except'塊中添加一個'else'子句來處理沒有明確異常的情況。 –

+0

或者,在'try'塊內移動'if else'鏈,因爲這是它在邏輯上屬於的地方。 –

回答

3

首先,您捕捉異常的方式無效。表達式ValueError or TypeError的值始終只是ValueError,因爲這是短路如何與兩個非參數一起工作的方式。要獲得這兩種類型的錯誤以觸發塊,請使用tuple,如(ValueError, TypeError)

問題是,即使在代碼中發現異常,它也會繼續到if塊。你有四個簡單的選項來避免這種情況:

  1. 使用continue聲明中except塊告訴循環繼續前進,而不處理以下if結構:

     
    try: 
        guess = float(input("Please guess a number between 0 and 100: ")) 
    except (ValueError, TypeError): 
        print ("That isn't even a number!") 
        continue 
    

    這可能是最乾淨和四個選項中最簡單的。

  2. 請勿使用except塊來回應錯誤。相反,依靠猜測的值仍然是""的事實。對於這個工作,你將不得不外循環與循環的每次迭代預初始化guess不是一次:

     
    while guess != number: 
        guess = "" 
        try: 
         guess = float(input("Please guess a number between 0 and 100: ")) 
        except (ValueError, TypeError): 
         pass 
        
        if guess == "": 
         print ("That isn't even a number!") 
        elif guess < number and guess >= 0: 
         ... 
    

    就個人而言,我不是這種方法的粉絲,因爲它需要在每一個初始化循環。這並不壞,只是沒有選項1那麼幹淨。

    此選項的變體是直接檢查guess是否爲str的實例。然後,可以將其初始化爲所述用戶輸入,使得轉換操作清潔器:

     
    while guess != number: 
        guess = input("Please guess a number between 0 and 100: ") 
        try: 
         guess = float(guess) 
        except (ValueError, TypeError): 
         pass 
        
        if isinstance(guess, str): 
         print ("That isn't even a number!") 
        elif guess < number and guess >= 0: 
         ... 
    
  3. 使用else子句是try塊的可能的元素之一。這一條款被執行只有如果沒有發生異常:

     
    try: 
        guess = float(input("Please guess a number between 0 and 100: ")) 
    except (ValueError, TypeError): 
        print ("That isn't even a number!") 
    else: 
        if guess < number and guess >= 0: 
         ... 
    

    雖然這個選項創建縮進的添加層,這是值得記住的那些情況下,一個普通的continue將無法​​正常工作的可能性。這種情況有時會在分支之前需要爲錯誤和非錯誤情況進行額外的處理。

  4. 將整個if塊放入try塊。這種方式只會在沒有錯誤的情況下執行。這是我最不喜歡的選擇,因爲我喜歡我的try塊儘可能減少,以避免捕捉我不打算的例外。在Python,try比Java等語言相對較少性能殺手,所以你簡單的情況下,這仍是一個選項:

     
    try: 
        guess = float(input("Please guess a number between 0 and 100: ")) 
        if guess < number and guess >= 0: 
         ... 
    except (ValueError, TypeError): 
        print ("That isn't even a number!") 
    
你`except`循環中
+0

4:在try塊中放置額外的東西的問題是您冒險捕捉您不想處理的異常。 – zstewart

+0

@zstewart。我補充說。感謝您指出。 –

+0

謝謝你的幫助!我的代碼現在工作完美無瑕! – Cubemaster

0

嘗試使用else語句。

您除了捕獲打印,但讓腳本繼續運行。即使捕獲物被擊中,它也將繼續所有的if語句。當除了命中時,你想要做的是跳過你函數的主要邏輯。使用try-catch-else-finally塊的ELSE子句。

import random 

def Normal_Guess(): 
    number = round(random.uniform(0.0, 100.0),2) 
    guess = "" 
    while guess != number: 
     try: 
      guess = float(input("Please guess a number between 0 and 100: ")) 
     except (ValueError, TypeError): 
      print ("That isn't even a number!") 
     else: 
      if guess < number and guess >= 0: 
       print ("You need to guess higher!") 
      elif guess > number and guess <= 100: 
       print ("You need to guess lower!") 
      elif guess == number: 
       print("Congratulations! You guessed the number!") 
      elif guess < 0 or guess > 100: 
       print ("The number you guessed is not between 0 and 100") 
      else: 
       print("That isn't even a number!") 

Normal_Guess() 
+0

'除了ValueError或TypeError:'必須是'除了(ValueError,TypeError):'正如瘋狂物理學家在他的回答中指出的那樣。 – rrauenza