2014-10-08 75 views
-1

好的,所以我一直在玩這段代碼。我想要它做的是:Wack A Python with Python Syntax/Punctuation

a)如果是,繼續到程序的下一部分。 b)如果不是,返回問題重新輸入正確的數據。 c)如果兩者都不是,則再次請求y/n答案。

所以現在這是我迄今爲止。我正在使用Python 3.4.1,現在它告訴我「else」語句後最後一個變量「answer」的「無效語法」。如果我試圖調整這個陳述,那麼它會繼續告訴我,冒號不合適,「elif」是「無效語法」,並且「if」語句中的第一個「break」是「out」循環「由於縮進。所以這裏是我的問題:我從哪裏開始調試它,因爲它似乎很困惑?

b = input ('Enter outstanding balance: ') 
i = input ('Enter annual interest rate as a decimal: ') 
m = input ('Enter monthly minimum payment as a decimal: ') 

print ('Your oustanding balance is: ' + b) 
print ('Your annual interest rate in decimal form is: ' + i) 
print ('Your monthly minimum payment as a decimal is: ' + m) 

answer = input('If this is correct please type: yes or no: ') 

if answer == ('no'): 
    print('You said no! Darn, let me get those numbers again...') 
    break 
elif answer == ('yes'): 
    print ('Great! Let us continue...') 
    continue 
else answer != ('yes', 'no'): 
    print ('You did not answer correctly! Please try again: ') 
    break 

任何和所有的答案將不勝感激! :)

回答

1

您無法指定else的條件(並且在此上下文中,它無論如何不是邏輯上需要的)。只要做到:

else: 
    print ('You did not answer correctly! Please try again: ') 

而且,break不是一個if塊正確的語法,所以從你的代碼中刪除所有的break秒。您可能會想到Python不支持的switch語句。

+0

我認爲這是在一個循環內(他沒有給我們看),而break和continue是用於循環的。 (如果不是,它應該全部放在循環中,因爲這是他得到這個結果的唯一方式,「返回問卷」部分。) – abarnert 2014-10-09 00:38:11

0

與switch語句不同,if-elif-else語句是互斥的,所以在輸入與該條件分支相對應的代碼塊時不需要中斷。此外,其他語句不能採取條件,因爲它們意味着(如果以上都不是真的)。你描述的行爲是一個循環,其中一段代碼被重複,直到滿足某個條件。使用一個標誌(一個布爾變量)和一個while循環,你可以運行該代碼塊任意次數,直到if-elif-else語句中的任何條件得到滿足,此時你改變標誌,這反過來終止循環。這裏是一個使用你的代碼的例子:

編輯:我重讀你的規格和修改代碼,使它重新要求是/否回答而不是再次獲得前三個輸入。

move_on= False 
while (!move_on): 
     b = input ('Enter outstanding balance: ') 
     i = input ('Enter annual interest rate as a decimal: ') 
     m = input ('Enter monthly minimum payment as a decimal: ') 

     print ('Your oustanding balance is: ' + b) 
     print ('Your annual interest rate in decimal form is: ' + i) 
     print ('Your monthly minimum payment as a decimal is: ' + m) 

     answer = input('If this is correct please type: yes or no: ') 

     ask_again= True 
     while (ask_again): 

       if answer == 'no': 
         print('You said no! Darn, let me get those numbers again...') 
         ask_again= False 
       elif answer == 'yes': 
         print ('Great! Let us continue...') 
         ask_again= False 
         move_on=True 
       else: 
         print ('You did not answer correctly! Please try again: ') 
+0

爲什麼不只是'而真'? – 2014-10-09 00:21:21

+0

你已經爲他的代碼添加了至少一個SyntaxError,以及額外的複雜性。 – abarnert 2014-10-09 00:39:25

+0

這不包含我的機器上的任何語法錯誤,但這可能是由於python版本(我正在使用3.2.3)的差異。我認爲使用帶有break和continue語句的無限循環並不是一個好主意;我更喜歡國旗的方式,但這只是我的看法。 – dramzy 2014-10-09 00:47:09

1

首先,你else是至少無效的原因:如果你想測試一個條件,你需要elif;如果要測試answer是否等於兩個值都不是not in,而不是!=;等等。但你真的沒有理由在這裏測試任何東西。如果您沒有執行ifelif,您需要執行else

其次,您錯過了while True:去與那些breakcontinue陳述。您還有breakcontinue落後。 A break跳出一個循環,並繼續與該程序的其餘部分;一個continue繼續下一次通過循環。

所以:

while True: 

    b = input ('Enter outstanding balance: ') 
    i = input ('Enter annual interest rate as a decimal: ') 
    m = input ('Enter monthly minimum payment as a decimal: ') 

    print ('Your oustanding balance is: ' + b) 
    print ('Your annual interest rate in decimal form is: ' + i) 
    print ('Your monthly minimum payment as a decimal is: ' + m) 

    answer = input('If this is correct please type: yes or no: ') 

    if answer == ('no'): 
     print('You said no! Darn, let me get those numbers again...') 
     continue 
    elif answer == ('yes'): 
     print ('Great! Let us continue...') 
     break 
    else: 
     print ('You did not answer correctly! Please try again: ') 
     continue 

目前還有一個問題留給:你想第三選擇回到剛纔再問最後一個問題,而不是整個事情。這意味着你需要在一個循環內循環......並且一次只能循環一次循環使用breakcontinue。這可能是值得將它重構成更小的函數的許多原因之一,所以當你完成後你可以只需要return。例如:

def yesno(prompt): 
    while True: 
     answer = input(prompt) 
     if answer == "no": 
      return False 
     elif answer == "yes": 
      return True 
     else: 
      print('You did not answer correctly! Please try again:') 

def questionnaire(): 
    while True: 
     b = input ('Enter outstanding balance: ') 
     i = input ('Enter annual interest rate as a decimal: ') 
     m = input ('Enter monthly minimum payment as a decimal: ') 

     print ('Your oustanding balance is: ' + b) 
     print ('Your annual interest rate in decimal form is: ' + i) 
     print ('Your monthly minimum payment as a decimal is: ' + m) 

     if yesno('If this is correct please type: yes or no:'): 
      print('Great! Let us continue...') 
      return b, i, m 
     else: 
      print('You said no! Darn, let me get those numbers again...') 

通知我並不需要breakcontinue任何地方。