Python Forum
Powerball assignment, how to get correct output of a dictionary ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Powerball assignment, how to get correct output of a dictionary ? (/thread-16909.html)



Powerball assignment, how to get correct output of a dictionary ? - msp1981 - Mar-19-2019

I have an assignment for python to have user input 5 white numbers and 1 red for powerball, so far heres what i've got, the problem i'm having is outputting the winning result, it is printing my entire dictionary and i don't know how to word this to have it give the proper key's output, i have also tried using if matches in Prizes: print(Prizes[macthes]) which yeilded nothing.

grand_prize = '$242,000,000'
Prizes = { '0, 1':'$4', '1, 1':'$4', '2, 1':'$7', 
          '3, 0':'$7', '3, 1':'$100', '4, 0':'$100',
          '4, 1':'$50,000', '5, 0':'$1,000,000', 
             '5, 1': grand_prize }

#WHITE and RED = winning numbers
WHITE = [2, 12, 16, 29, 54]
RED = 6
white_nums = 0
red_nums = 0


nums = input('Please enter your 5 white balls 1-69 and red ball 1-26:')
user_nums = nums.split()

for x in range(0, 4):
    if int(user_nums[x]) in WHITE:
        white_nums += 1
if int(user_nums[-1]) == RED:        
    red_nums += 1
       

matches = (white_nums, red_nums)

for matches in Prizes:
    print(Prizes[matches])



RE: Powerball assignment, how to get correct output of a dictionary ? - Yoriz - Mar-19-2019

matches = (white_nums, red_nums)
creates a tuple that wont match with the keys in the dictionary that are strings.
change this to
matches = f'{white_nums}, {red_nums}'
 for matches in Prizes:
     print(Prizes[matches])
is just looping through the whole dictionary and overwrites the previous assignment to matches
remove those lines and replace with
result = Prizes.get(matches, 'sorry no prize this time')
print(result)
this will grab the value if the key matches exists or return the the string i gave as a default.

Also note there is an error in the size of your range
for x in range(0, 4):
its not using all the expected input values.


RE: Powerball assignment, how to get correct output of a dictionary ? - msp1981 - Mar-19-2019

Thank you so much, the reason I have the range set the way I do, is his instructions stated the numbers will be input with a space between them, the last number will be the powerball, so i had the range 0, 4 to get the white ones only and the user_nums[-1] to get the red, is this not correct?


RE: Powerball assignment, how to get correct output of a dictionary ? - Yoriz - Mar-19-2019

for x in range(0, 4):
    print(x)
Output:
0 1 2 3
only 4 numbers


RE: Powerball assignment, how to get correct output of a dictionary ? - msp1981 - Mar-19-2019

thanks. dunno what to say to that, feel stupid now i think i originally had it at 5 also i've been messing around with this all day


RE: Powerball assignment, how to get correct output of a dictionary ? - Yoriz - Mar-19-2019

Don't worry its an easy mistake and we learn from our mistakes :)