如何打印用户选择的内容


black_v = []
red_v = []
green_v = []
yellow_v = []
blue_v = []
magenta_v = []
cyan_v = []
white_v = []
nums_8 = ['1', '2', '3', '4', '5', '6', '7', '8']
print("nu001b[31mnEMEGENCY MEETING")
vote = input("nu001b[37mWho do you vote for?:n[1] Blackn[2] Redn[3] Greenn[4] Yellown[5] Bluen[6] Magentan[7] Cyann[8] WhitenEnter the number (1-8):>>> ")
while vote in nums_8:
if vote == '1':
black_v.append(1)
break

我的问题是,与其为每个数字都做一个"if语句",我有没有办法只写下一些东西,如果用户输入"1",程序会在"black_v"后面加1,如果他输入"3",程序就会在"green_v"前面加1。告诉我如何在Python 3.x而不是2中做到这一点。

使用字典而不是单独的变量。

votes = {
"black": [],
"red": [],
"green": [],
"yellow": [],
"blue": [],
"magenta": [],
"cyan": [],
"white": []
}
print("nu001b[31mnEMEGENCY MEETING")

while True:
vote = input("nu001b[37mWho do you vote for? (black, red, green, yellow, blue, magenta, cyan, white) >>> ")
if vote in votes:
votes[vote].append(1)
break
else:
Print("That's not a valid vote, try again")

我会这样做。避免";硬编码";只要有可能。这样,你也可以在labels中添加更多的值,它将一如既往地工作(就像我添加了<skip>值一样(

labels = ['Black', 'Red', 'Green', 'Yellow', 'Blue', 'Magenta', 'Cyan', 'White', '<skip>']
num_to_labels = { str(num + 1):label for num, label in enumerate(labels)}
num_to_votes =  { str(num + 1):0     for num, label in enumerate(labels)}
print('nu001b[31mnEMERGENCY MEETING')
print('nu001b[37mWho do you vote for?:')
for num, label in num_to_labels:
print(f'[{num}] {label}')
vote = input(f'Enter the number (1-{len(labels)}):>>> :')
while not vote in num_to_votes.keys():
vote = input(f'Wrong number, repeat (1-{len(labels)}):>>> :')
num_to_votes[vote] += 1

试试这个

candidates = [['black_v', 0],['red_v', 0],['green_v', 0], ['yellow_v', 0], ['blue_v', 0], ['magenta_v',0], ['cyan_v',0], ['white_v', 0]]

print("nu001b[31mnEMEGENCY MEETING")
vote = input("nu001b[37mWho do you vote for?:n[1] Blackn[2] Redn[3] Greenn[4] 
Yellown[5] Bluen[6] Magentan[7] Cyann[8] WhitenEnter the number (1-8):>>> ")
candidates[int(vote) - 1][1] += 1

输入

EMEGENCY MEETING
Who do you vote for?:
[1] Black
[2] Red
[3] Green
[4] Yellow
[5] Blue
[6] Magenta
[7] Cyan
[8] White
Enter the number (1-8):>>> 1

输出

print(candidates)

[['black_v', 1], ['red_v', 0], ['green_v', 0], ['yellow_v', 0], ['blue_v', 0], ['magenta_v', 0], ['cyan_v', 0], ['white_v', 0]]

colors = {"Black": [], "Red": [], "Green": [], "Yellow": [], "Blue": [], "Magenta": [], "Cyan": [], "White": []}
_map_of_colors = {num: color for num, color in enumerate(colors, start=1)}
# {1: 'Black', 2: 'Red', 3: 'Green', 4: 'Yellow', 5: 'Blue', 6: 'Magenta', 7: 'Cyan', 8: 'White'}
msg_colors_options = 'n'.join([f'[{key}] {color}' for key, color in _map_of_colors.items()])
# [1] Black
# [2] Red
# [3] Green
# [4] Yellow
# [5] Blue
# [6] Magenta
# [7] Cyan
# [8] White
# f-string only python3.6+
msg_input = f"""u001b[31m
EMEGENCY MEETING
u001b[37m
{msg_colors_options}
Enter the number (1-8):>>>"""
#
#
# EMEGENCY MEETING
#
# [1] Black
# [2] Red
# [3] Green
# [4] Yellow
# [5] Blue
# [6] Magenta
# [7] Cyan
# [8] White
# Enter the number (1-8) or 0 to exit:>>>
while True:
option = input(msg_input)
if not option.isalnum() or int(option) not in range(len(_map_of_colors)):
input(f"u001b[31mValue {option} isn't valid.u001b[37mnPress <ENTER> to continue.")
continue
# cast int
option = int(option)
if option == 0:
# show values
print("Values: ", colors)
print("Goodbye!")
break
# get color name in map
color_name = _map_of_colors[option]
# get color array by name and append.
colors[color_name].append(option)

做了一些更新

你可以试试这个方法,把你的列表放在python字典里,它的关键字是用户输入的数字值。因此,无论用户输入什么,它都会得到对应于特定数字的列表,然后appends。很抱歉,这个列表不是一个好主意。。。我知道有什么不对劲,我想的是一本字典。

看看示例输出,当您输入2时,它将把输入数据附加在red_v列表上,因为它是2,所以在字典上,值为red_v列表的关键字2append作为用户的输入。

black_v = []
red_v = []
green_v = []
yellow_v = []
blue_v = []
magenta_v = []
cyan_v = []
white_v = []
nums_8 = ['1', '2', '3', '4', '5', '6', '7', '8']
vs = {
1:black_v,
2:red_v,
3:green_v,
4:yellow_v,
5:blue_v,
6:magenta_v,
7:cyan_v,
8:white_v
}

print("nu001b[31mnEMEGENCY MEETING")
vote = input("nu001b[37mWho do you vote for?:n[1] Blackn[2] Redn[3] Greenn[4] Yellown[5] Bluen[6] Magentan[7] Cyann[8] WhitenEnter the number (1-8):>>> ")

vs[int(vote)].append(vote)

print("Output in red_v list")
print(vs[2])


'''
while vote in nums_8:
if vote == '1':
black_v.append(1)
break
'''

输出:

[1] Black
[2] Red
[3] Green
[4] Yellow
[5] Blue
[6] Magenta
[7] Cyan
[8] White
Enter the number (1-8):>>> 2
Output in red_v list
['2']

最新更新