Python的新功能需要有关IF语句的一些指导

时间:2018-07-03 15:57:29

标签: python python-3.x

Q:代码运行,但仅输出else语句...    任何帮助表示赞赏。

fruit = str()
favorite_fruits = ["mango", "kiwi", "pineapple"]
if fruit in favorite_fruits is "mango":
    print('You really like ' + fruit + '.')
if fruit in favorite_fruits is "kiwi":
    print('You really like ' + fruit + '.')
if fruit in favorite_fruits is "pineapple":
    print('You really like ' + fruit + '.')
if fruit in favorite_fruits is "apple":
    print('You really like' + fruit + '.')
else:
    print('Your going to starve.')

1 个答案:

答案 0 :(得分:0)

str()每次返回''。因此,fruit始终是'',不满足任何if语句。

您的if语句也已关闭。您可以只检查fruit中是否包含favorite_fruits,然后打印:

fruit = "mango"
favorite_fruits = ["mango", "kiwi", "pineapple"]

if fruit in favorite_fruits:
    print('You really like ' + fruit + '.')
else:
    print('Your going to starve.')

如果要用户输入,请使用input而不是硬编码fruit