2012-10-17 58 views
0
#!/usr/bin/env python 
import easygui as eg 


start = eg.msgbox('In your head, pick a number between 1 and 100. No matter what, I will guess it in no more than 7 tries. When you think of a number, press OK to start the program.') 

maximum = 100 
minimum = 0 
middle = (maximum-minimum)/2 
attempt= 1 
while True: 
    if attempt > 7: 
     eg.msgbox('You cheated. Terminating...') 
     break 
    else: 
     yon = eg.boolbox("Is your number: " + str(middle) + '?', 'Guess Result',('Yes','No')) 
     if yon == 1: 
      eg.msgbox("Found in " + str(attempt) + " try!") 
      break 
     if yon == 0: 
      choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low') 
     if choice == 0: 
      minimum = middle 
      middle = int(round(float(maximum+minimum)/2)) 
     elif choice == 1: 
      maximum = middle 
      middle = int(round(float(maximum+minimum)/2)) 
     else: 
      eg.msgbox"Enter valid input!";"Lets start again from the last step" 
      continue 
     attempt+= 1 

好吧,我不斷收到第20行的縮進錯誤。我無法弄清楚。我的語法顯示正常。我回去並刪除了所有縮進並重新縮進它們(以確保組合中沒有空格)。爲什麼它給了我這個?我該如何解決它?EasyGUI縮進錯誤,我不知道爲什麼

最讓我困惑的是我寫出了相同的代碼,但沒有導入EasyGUI模塊並且沒有EasyGUI修改,但它與此相同。 if/elif/else在同一個位置,除了'eg.msgbox'省略'print'命令並且raw_input已被替換爲'boolbox'之外,所有內容都是相同的。

編輯 - 第20行是:

if yon == 0: 

回答

1
if yon == 0: 
    choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low') 

您已經錯過了一個右括號。

choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low')) 

此外,請檢查您的最後else:。它應該是: -

else: 
    eg.msgbox("Enter valid input!","Lets start again from the last step") 
    continue 

您還沒有縮進你最後if正確: -

if yon == 0: 
     choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low') 
    if choice == 0: 
     minimum = middle 
     middle = int(round(float(maximum+minimum)/2)) 
    elif choice == 1: 
     maximum = middle 
     middle = int(round(float(maximum+minimum)/2)) 
    else: 
     eg.msgbox"Enter valid input!";"Lets start again from the last step" 
     continue 

您所有的if-elif-else應該是你的外表,如果裏面。您正在檢查變量內定義的條件..所以重新縮進您的結構: -

if yon == 0: 
    choice = eg.boolbox("Was my guess..." , 'High or Low?' , ('High' , 'Low') 
    if choice == 0: 
     minimum = middle 
     middle = int(round(float(maximum+minimum)/2)) 
    elif choice == 1: 
     maximum = middle 
     middle = int(round(float(maximum+minimum)/2)) 
    else: 
     eg.msgbox"Enter valid input!";"Lets start again from the last step" 
     continue 
+0

絕對。很高興你指出了。但是,縮進錯誤發生在上面的線上,我敢肯定,如果問題變得那麼嚴重,問題本身就會出現。 – 01110100

+0

@ 01110100那可能是因爲那個支架..你永遠不知道。 –

+0

@ 01110100我做了這個改變,它工作得很好:) –

相關問題