if语句中的if语句
问题描述:
这是有点我的功课,我坚持让代码成为后续问题,就像这里的代码一样,我试过在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)
答
你有一个额外的缩进。只需删除额外的缩进级别:
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)
答
缩进是在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:
,有多余的空格。
如果它说有一个意外的缩进,你看了看那条线的缩进,并考虑它是否可以站得住脚吗? – TigerhawkT3