Python Forum
Powerball assignment, how to get correct output of a dictionary ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powerball assignment, how to get correct output of a dictionary ?
#1
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])
Reply
#2
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.
Reply
#3
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?
Reply
#4
for x in range(0, 4):
    print(x)
Output:
0 1 2 3
only 4 numbers
Reply
#5
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
Reply
#6
Don't worry its an easy mistake and we learn from our mistakes :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  to find in dictionary given parameter 'name' and to output position Liki 10 1,231 Oct-08-2023, 06:38 AM
Last Post: Pedroski55
  dictionary output to text file (beginner) Delg_Dankil 2 1,131 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Powerball List jackthechampion 1 2,442 Apr-13-2020, 06:02 PM
Last Post: deanhystad
  Dictionary based assignment: Need help RoyceJ 6 3,817 Aug-29-2018, 05:30 AM
Last Post: perfringo
  Powerball script help nick 6 6,936 Nov-11-2016, 07:56 PM
Last Post: micseydel
  PowerBall Help nick 3 4,729 Nov-11-2016, 06:59 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020