首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >if语句中的if语句

if语句中的if语句
EN

Stack Overflow用户
提问于 2016-08-17 10:57:02
回答 2查看 54关注 0票数 1

这是我的家庭作业,我坚持让代码变成一个后续问题,就像这里的代码,我试着在后面插入一个if语句,但它给了我一个意想不到的提示错误。

到目前为止,我的代码如下:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

发布于 2016-08-17 10:58:58

你有一个额外的缩进。只需删除额外的缩进级别:

代码语言:javascript
复制
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)
票数 4
EN

Stack Overflow用户

发布于 2016-08-17 11:01:15

缩进是在Python中创建代码块的方法。你的错误恰好说明了你做错了什么。

代码语言:javascript
复制
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:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38987390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档