Aug-31-2021, 07:08 PM
I liked this. I did a version.
#! /usr/bin/env python3 # Do the imports import random as rnd # Get the lottery numbers lottery_nums = rnd.sample(range(1, 61), 5) # Sort the lottery numbers for display lottery_nums.sort() # Create empty list user_nums = [] # Message to display in various areas message = 'Please enter a number between 1 and 60' print(message) # Start a while loop. Will end when we have 5 numbers in our list while len(user_nums) < 5: num = input('>> ') try: # convert input to int num = int(num) except ValueError: # Throw error if not in the correct format print(f'{num} is not a number. {message}') continue # Check that our numbers are withn the correct range if num < 1 or num > 60: print(f'That number is not in range. {message}.') continue # Check if the number has already been picked if num in user_nums: print(f'You have already picked {num}. Please try again.') continue else: # Append the number to user picks user_nums.append(num) # Sort user numbers for display user_nums.sort() # Check for matches in the two list matches = len([key for key, val in enumerate(lottery_nums) if val in set(user_nums)]) # Display information print(f'Lottery Numbers: {lottery_nums}') print(f'Player Numbers: {user_nums}') print(f'You matched {matches} number(s)')
Output:Please enter a number between 1 and 60
>> 20
>> 15
>> 15
You have already picked 15. Please try again.
>> 33
>> 61
That number is not in range. Please enter a number between 1 and 60.
>> 60
>> 14
Lottery Numbers: [26, 43, 44, 51, 60]
Player Numbers: [14, 15, 20, 33, 60]
You matched 1 number(s)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags