Python 如何让用户从列表中的一个值中进行选择

Python 如何让用户从列表中的一个值中进行选择,python,Python,在第5行中,我如何使它以这样的方式,如果偏好==一个水果而不是全部?另外,输入返回一个字符串,对吗?如何让用户从变量中选择一个值?我是新来的,所以如果我犯了愚蠢的错误,请原谅我,谢谢 fruits = ['banana', 'pineapple', 'strawberry', 'mango'] preference = input('What fruit would you like?') if preference == 'nothing': print('Sorry to hear

在第5行中,我如何使它以这样的方式,如果偏好==一个水果而不是全部?另外,输入返回一个字符串,对吗?如何让用户从变量中选择一个值?我是新来的,所以如果我犯了愚蠢的错误,请原谅我,谢谢

fruits = ['banana', 'pineapple', 'strawberry', 'mango']
preference = input('What fruit would you like?')
if preference == 'nothing':
    print('Sorry to hear that, goodbye!')
elif preference == fruits:
    print('That\'s great! I\'ll be back in a moment with your order.')
    if preference == preference[0]:
        print(f'Here you go! That would be {len(preference[0])} dollars')
    elif preference == preference[1]:
        print(f'Here you go! That would be {len(preference[1])} dollars')
    elif preference == preference[2]:
        print(f'Here you go! That would be {len(preference[2])} dollars')
    elif preference == preference[3]:
        print(f'Here you go! That would be {len(preference[3])} dollars')
    print('Thank you for your purchase! Goodbye!')
else:
    print(f'Here\'s our menu! We have {fruits[1:3]} and {fruits[-1]} here.')

在中使用关键字

水果中的elif偏好:
您还应该检查水果中每个值的每个偏好。不是对自己。同样的事情也适用于打印首选项的长度。您为您的首选项编制了索引,这意味着如果您的首选项为“香蕉”,则将采用第0个索引或“b”。它的值始终为1。更正代码:

水果=['香蕉'、'菠萝'、'草莓'、'芒果']
首选项=输入('您想要什么水果?')
如果首选项==“无”:
print('很抱歉听到这个消息,再见!')
水果中的elif偏好:
打印('太好了!我马上就回来拿您的订单。')
如果首选项==水果[0]:
打印(给你!那将是{len(优惠)}美元')
elif首选项==水果[1]:
打印(给你!那将是{len(优惠)}美元')
elif首选项==水果[2]:
打印(给你!那将是{len(优惠)}美元')
elif首选项==水果[3]:
打印(给你!那将是{len(优惠)}美元')
打印('谢谢您的购买!再见!')
其他:
打印(这是我们的菜单!我们这里有{水果[1:3]}和{水果[-1]})
这意味着你根本不需要检查它是哪种水果!这会让你更简单。像这样:

水果=['香蕉'、'菠萝'、'草莓'、'芒果']
首选项=输入('您想要什么水果?')
如果首选项==“无”:
print('很抱歉听到这个消息,再见!')
水果中的elif偏好:
打印('太好了!我马上就回来拿您的订单。')
打印(给你!那将是{len(优惠)}美元')
打印('谢谢您的购买!再见!')
其他:
打印(这是我们的菜单!我们这里有{水果[1:3]}和{水果[-1]})
您需要在中使用
,检查
是否在
列表中


另外,
水果
是一个列表,您可以在其中检查
首选项
,因此使用
水果
而不是
首选项

为什么它仍然不起作用?它没有显示价格。我已经更新了另一个错误的答案。哦,我的上帝!非常感谢你!这是如此的简短和简单,谢谢!!哎呀,我差点忘了,最后一个问题,如果你不介意的话。最后一行返回这是我们的菜单!我们这里有菠萝、草莓和芒果。为什么?如何使其不显示括号?要使用分隔符连接列表,例如“,”,我们需要这样做:
“,”。连接(列表)
。这将返回“菠萝,草莓”。所以我把第五行改为“水果优先”?我这样做了,但它说的是“预期类型”str,而不是“列表[str]”。还有,为什么不显示价格?您正在检查输入首选项是否在水果列表中。所以使用
如果优先选择水果
而不是
优先选择水果
啊,我明白了。非常感谢你!
# your code goes here

fruits = ['banana', 'pineapple', 'strawberry', 'mango']
preference = input('What fruit would you like?')
if preference == 'nothing':
    print('Sorry to hear that, goodbye!')
elif preference in fruits:
    print('That\'s great! I\'ll be back in a moment with your order.')
    if preference == fruits[0]:
        print(f'Here you go! That would be {len(fruits[0])} dollars')
    elif preference == fruits[1]:
        print(f'Here you go! That would be {len(fruits[1])} dollars')
    elif preference == fruits[2]:
        print(f'Here you go! That would be {len(fruits[2])} dollars')
    elif preference == fruits[3]:
        print(f'Here you go! That would be {len(fruits[3])} dollars')
    print('Thank you for your purchase! Goodbye!')
else:
    print(f'Here\'s our menu! We have {fruits[1:3]} and {fruits[-1]} here.')