2011-06-06 324 views
0

正如你所看到的,我是編程中的新手,並且使用Python開始,上述錯誤發生在突出顯示的代碼行上。 如何駕馭此...Python錯誤:Unindent不匹配任何外部Uindentation限制

import random 

secret= random.randint (1,100) 
guess=0 
tries=0 

print "AHOY! I am the dead pirate Roberts, and I ahve a secret!" 
print "It is a number from 1 to 99. I will give you six tries." 

while guess !=secret and tries <6: 
    guess= input("what's yer guess? ") 
    if guess < secret : 
       print "Too Low, ye curvy dog!" 
    elif guess > secret: 
       print "Too high, landlubber!" 
       tries= tries +1 

     ***if guess == secret :*** 
      print "Avast! Ye got it! found my secret, ye did!" 
       else: 
      print "No more guesses! Better luck next time, matey!" 
      print "The secret number was ", secret" 

回答

2

Python使用縮進來表示代碼塊。代碼的縮進無效(所涉及的if的縮進與以前的任何塊都沒有對齊;從快速查看代碼,至少還有一個錯誤)。

以下是縮進在Python如何工作的一個簡短明確的解釋:http://diveintopython.net/getting_to_know_python/indenting_code.html

+0

非常感謝,感激...... – 2011-06-06 09:33:43

0

在Python中,你必須保持你的塊之間的恆定的壓痕。

而在if guess < secret:代碼塊中,縮進比在while代碼塊中長得多。

正確的代碼是:

while guess !=secret and tries <6: 
    guess= input("what's yer guess? ") 
    if guess < secret : 
     print "Too Low, ye curvy dog!" 
    elif guess > secret: 
     print "Too high, landlubber!" 
     tries= tries +1 

    if guess == secret : 
     print "Avast! Ye got it! found my secret, ye did!" 
    else: 
     print "No more guesses! Better luck next time, matey!" 
     print "The secret number was ", secret" 
+0

非常感謝,我明白了...固定! – 2011-06-06 09:32:12

相關問題