这是我的家庭作业,我坚持让代码变成一个后续问题,就像这里的代码,我试着在后面插入一个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)发布于 2016-08-17 10:58:58
你有一个额外的缩进。只需删除额外的缩进级别:
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)发布于 2016-08-17 11:01:15
缩进是在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:。
https://stackoverflow.com/questions/38987390
复制相似问题