Reputation: 55
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("\n\u001b[31m\nEMEGENCY MEETING")
vote = input("\n\u001b[37mWho do you vote for?:\n[1] Black\n[2] Red\n[3] Green\n[4] Yellow\n[5] Blue\n[6] Magenta\n[7] Cyan\n[8] White\nEnter the number (1-8):>>> ")
while vote in nums_8:
if vote == '1':
black_v.append(1)
break
My question is instead of making a 'if statement' for every number, is there a way I can just write something down and if the user enters '1', the program will append 1 to 'black_v' and if he enters '3', the program appends 1 to 'green_v'. Tell me how do do this in Python 3.x not 2.
Upvotes: 0
Views: 85
Reputation: 782584
Use a dictionary instead of separate variables.
votes = {
"black": [],
"red": [],
"green": [],
"yellow": [],
"blue": [],
"magenta": [],
"cyan": [],
"white": []
}
print("\n\u001b[31m\nEMEGENCY MEETING")
while True:
vote = input("\n\u001b[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")
Upvotes: 1
Reputation: 4086
You could try this where you can put your list in a python dictionary and its key is the number value inputted by the user. So whatever the user enters is it gets the list in correspond to that particular number then appends
it. Sorry the list wasn't a good idea... I knew something was off and the thing I was thinking was a dictionary.
Take a look at the sample output where when you enter 2
it will append the input data on the red_v
list since it is 2
so on the dictionary the key 2
with a value of a red_v
list will append
the input of the user.
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("\n\u001b[31m\nEMEGENCY MEETING")
vote = input("\n\u001b[37mWho do you vote for?:\n[1] Black\n[2] Red\n[3] Green\n[4] Yellow\n[5] Blue\n[6] Magenta\n[7] Cyan\n[8] White\nEnter 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
'''
Output:
[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']
Upvotes: 1
Reputation: 84
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[37m\nPress <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)
Upvotes: 1
Reputation: 491
Try this
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("\n\u001b[31m\nEMEGENCY MEETING")
vote = input("\n\u001b[37mWho do you vote for?:\n[1] Black\n[2] Red\n[3] Green\n[4]
Yellow\n[5] Blue\n[6] Magenta\n[7] Cyan\n[8] White\nEnter the number (1-8):>>> ")
candidates[int(vote) - 1][1] += 1
Input
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
Output
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]]
Upvotes: 1
Reputation: 7937
I would do it like this. Avoid "hardcoding" when it's possible. This way you can also add some more values in labels
and it'll work as always (like I added a <skip>
value)
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('\n\u001b[31m\nEMERGENCY MEETING')
print('\n\u001b[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
Upvotes: 1