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.')
答案 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
。