我的代码类似于:
user = input("Enter username: ")
我有10个变量,比如:
ch1 = "String1"
ch2 = "String2"
ch3 = "String3"
ch4 = "String4"
...
假设输入的用户名是Pawan,我的input语句返回一个列表:
basic_choices = input("Enter All your choices separated by ',' to perform in your remote system together: ").strip().split(',')
输出是列表中从1到10的一些随机数:
['1','3','7']
现在,我想根据用户的选择在一行中打印一个字符串:
对于1,3,7,is应该给出输出:
The strings selected by you are String1; String3; String7; in the strings list of Pawan
(应该包括分号)我尝试了很多方法,但都不起作用,要么只返回第一个数字的值,要么返回地址处的生成器对象
print("The strings selected by you are ch{0}; in the strings list of {1}".format(*basic_choices, user))
print("The strings selected by you are ch{0}; in the strings list of {1}".format(choice, user) for choice in basic choices)</p>
发布于 2020-06-09 03:58:19
我认为将“字符串”放在字典中是有意义的,因为字典中的键是可能的用户输入。然后我使用str.join
对输出进行了正确的格式化:
user = 'Pawan'
ch1 = "String1"
ch2 = "String2"
ch3 = "String3"
ch4 = "String4"
ch5 = "String5"
str_options = {'1':ch1,'2':ch2,'3':ch3,'4':ch4,'5':ch5}
basic_choices = input("Enter All your choices separated by ',' to perform in your remote system together: ").strip().split(',')
chosen = [str_options[i] for i in basic_choices]
print("The strings selected by you are " +
"; ".join(chosen) +
"; in the strings list of {}".format(user))
请注意,(按原样)如果用户输入不在字典中,这将引发错误。
https://stackoverflow.com/questions/62274290
复制