2016-08-17 151 views
1

這是有點我的功課,我堅持讓代碼成爲後續問題,就像這裏的代碼一樣,我試過在if語句之後插入if語句,但它給了我一個意想不到的出現錯誤。if語句中的if語句

這裏是我的代碼至今:

Choice = input("Hello! Do you want to buy or sell snakes? Please enter (b) or (s) :") 
if Choice == "b": 
    buySnake = input ("What snake do you want to buy? We are selling python, kingsnake, rattlesnake, deathadder, cobra, mamba, viper and gartersnake. Please choose one : ") 
     if buySnake == "python": 
      return (to be added) 
elif Choice == "s": 
    sellFsnakes(snakeList,name,amount) 
else: 
    print("Please try again.") 
    buyWsnakes(snakeList,name,amount) 
+0

如果它說有一個意外的縮進,你看了看那條線的縮進,並考慮它是否可以站得住腳嗎? – TigerhawkT3

回答

4

你有一個額外的縮進。只需刪除額外的縮進級別:

Choice = input("Hello! Do you want to buy or sell snakes? Please enter (b) or (s) :") 
if Choice == "b": 
    buySnake = input ("What snake do you want to buy? We are selling python, kingsnake, rattlesnake, deathadder, cobra, mamba, viper and gartersnake. Please choose one : ") 
    if buySnake == "python": 
     return (to be added) 
elif Choice == "s": 
    sellFsnakes(snakeList,name,amount) 
else: 
    print("Please try again.") 
    buyWsnakes(snakeList,name,amount) 
0

縮進是在Python中創建代碼塊的方式。你的錯誤確切地說明你做錯了什麼。

if condition: 
    # executes if condition is met 
    if internalCondition: 
     # executes if both condition and internalCondition is met 
elif otherCondition: 
    # executes if first statement didn't and otherCondition is met 
else: 
    # executes if nothing else executed 

您縮進了if internalCondition:,有多餘的空格。