I am trying to code a guessing game in Python. I have a function called bingo_calculator() that takes a 12-character Y/N string as a parameter (i.e. YNNYNNYNYYYN) from a dictionary value and then returns an integer value based on the rules of the game.
My approach is to make a copy of the the dictionary (so that the original guesses are not overwritten) and have the function calculate the integer value and replace the string in the copied dictionary with the new calculated value. From there, I want to sort the integers in descending order. I cannot figure out how to make it so that the values of the dictionary update corresponding to the output of the function automatically.
bingo_entries_190_copy = bingoGuesses190.bingo_entries_190.copy() #makes a copy of the dictionary
for value in bingo_entries_190_copy.values():
print(bingo_calculator(value)) # outputs the integers on the terminal
Here's a sample of the dictionary:
bingo_entries_190_copy = {
'Craig': 'NNNNNYNYNYYY',
'Hirohito': 'YNNNNNNYNYNN',
'Elemér': 'NYNNYNYYNYYY',
'Muddy': 'NNYNYNYYNNYY',
'Kamen': 'NNNNNYYYYYYN',
'Hiram': 'NNYNNNYNNNYY',
'Rin': 'NYNNNYYBYNNN',
'Gessica': 'YNNNNYNYNYNN',
'Pavlina': 'NNNYNYNNNNYY'
}
The desired output would be:
bingo_entries_190_copy = {
'Muddy': 480,
'Pavlina': 430
'Hiram': 380,
'Craig': 160,
'Elemér': 160,
'Hirohito': 30,
'Gessica': 10,
'Kamen': -30,
'Rin': -110,
}
where the integers are returned when the 12-character Y/N strings are passed through bingo_calculator(). And then I need these entries sorted in descending order based on the integers.
What do I do?
CodePudding user response:
Is this what you're looking for?
guess_results = {}
for key in bingoGuesses190:
guess_results[key] = bingo_calculator(bingoGuesses190[key])
CodePudding user response:
You could simply map a new value to each of the name keys in the dictionary. Here's how I'd edit your code to do that:
bingo_entries_190_copy = bingoGuesses190.bingo_entries_190.copy() #makes a copy of the dictionary
for key in bingo_entries_190_copy.keys():
bingo_entries_190_copy[key] = bingo_calculator(bingo_entries_190_copy[key])
I hope that helped:)