Among Us Voting System: HALP AGAIN
Hi repl.it
Let's say I have the following code:
votes = {"red":2,"yellow":1,"green":0,"cyan":2,"blue":1,"magenta":0}
It's a dictionary named "votes". Now I want a function that can return the key names of the values that have the maximum value. If this is unclear, the function should return
ejected = ["red","cyan"]
since "red" and "cyan" have the biggest values in the dictionary. Can someone help me implement this?
Thanks in advance!
You might want to do this:
votes = { "red": 3, "yellow": 1, "green": 0, "cyan": 3, "blue": 1, "magenta": 0 } def getKey(val): for k, v, in votes.items(): if val == v: return k new_list = [] for key, value in votes.items(): new_list.append(value) ejected = [] max_value = max(new_list) ejected.append(getKey(max_value)) print(ejected)
it will only give you the color with the largest votes
it will only return one colors, not two
@SUHASTADIPARTH1 Thanks! :) My goal is to return all the ones with the highest value, but thanks for the code!
@SUHASTADIPARTH1 The objective is to return both colors, not just one.
@heyitsmarcus np. Mark my answer?
max(listName)
@adsarebbbad votes
is a dictionary so you have to do a little bit extra and grab the max from the values
in the dict
object:
maximum = max(votes.values())
@RYANTADIPARTHI max does not work in this instance. If you use max(dict, key = dict.get)
you can get the first key it encounters that is the max. However, this doesn't work for multiple keys.
Just one way to get the max is to use the max
function on the values:
maximum = max(votes.values())
It looks like you're using Python, so I wrote up a script, rife with comments for you to check through how I did it.
Let me know if you have any questions!
@heyitsmarcus Thanks! I have one more problem, wait a sec.
@Smart0ne no problem!
Hello,
(using Python)
I have one more problem. Let's define a few more variables.
I currently have code that turns the "colour" list (which has the people that are not dead) into the "vote_number" dictionary. So after the code is executed, we have
Now, I let the user choose who they vote for. I first display all the possible voting choices
And if they type in the number corresponding to a colour, I need to add one to the value in the dictionary. For example, if the user types in "3", I would have to execute
But ... I'm not sure how to do this. Currently, my code is very static, so I get errors like "Key Error".
How would I go implementing this?
@Smart0ne Just make a quick number to string dictionary:
And then you can enter the input directly to get the color
@Smart0ne if you found my example suited you, please mark my answer as the accepted answer so we can resolve this question. =]
@heyitsmarcus Hm ... after trying your code, I get a "TypeError: 'dict' object not callable".
Also, my code is
@Smart0ne
vote_number[vote_number_lookup(voting)]
should bevote_number[vote_number_lookup[voting]]
@fuzzyastrocat Oops. I got myself confused. :P Thanks!
@Smart0ne No problem!
I'd also like to note that while @heyitsmarcus 's answer works it's a bit inefficient since it iterates through the dictionary twice. A more efficient version would be like this:
@Smart0ne Actually, an even more efficient version would be as follows, since (as I understand it) Python slices don't copy values, they just make a reference:
@fuzzyastrocat YES IT WORKED! I'm not sure who to mark as answer though ...
@Smart0ne Lol I don't care, all I care is that it worked
oh come on it's resolved
ok whatever happy coding @fuzzyastrocat
As long as everything works that is great, but I originally looped through twice before I refactored.
@heyitsmarcus That's looping through twice:
max
iterates through the entire list, then you iterate through with afor
loop.