Python 3.x 在Python3.x中使用for循环显示请求用户输入的列表
我有以下清单:Python 3.x 在Python3.x中使用for循环显示请求用户输入的列表,python-3.x,list,for-loop,input,Python 3.x,List,For Loop,Input,我有以下清单: [(1,'碳'),(2,'氮'),(3,'磷')] 用户将选择一个代表其选择的数字,例如,如果用户想要选择碳,则选择1 使用输入法,我希望用户看到如下内容: Please select the number of your choice: 1 Carbon 2 Nitrogen 3 Phosphorus ***space for user input*** 然后输入保存在变量中 目前,我通过在输入语句之前使用for循环打印列表来实现这一点,但这对我来说还不够优雅 有没有办
[(1,'碳'),(2,'氮'),(3,'磷')]
用户将选择一个代表其选择的数字,例如,如果用户想要选择碳,则选择1
使用输入法,我希望用户看到如下内容:
Please select the number of your choice:
1 Carbon
2 Nitrogen
3 Phosphorus
***space for user input***
然后输入保存在变量中
目前,我通过在输入语句之前使用for循环打印列表来实现这一点,但这对我来说还不够优雅
有没有办法通过使用输入和for-loop方法来实现这一点?还是另一种方法
感谢您的帮助我没花太多时间就明白了,解决方案可能只是在开始时放置一条打印语句来模拟输入:
print('Select your choice')
for x in my_list:
print(x[0],x[1])
choice = input()
l = [(1, 'Carbon'), (2, 'Nitrogen'), (3, 'Phosphorus')]
s ='\n'.join([' '.join([str(i) for i in x]) for x in l])
print("Please select the number of your choice: \n\n{}".format(s))
user_input = int(input("user input : "))