Create an app where the user will enter 5 random number ranging from 1-20 and message will display about money won according to the number of matched numbers. You must use the set data structure of python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random # random module to gererate numbers | |
def menu(): # start our app | |
# ask player for numbers | |
user_numbers = get_player_numbers() # calling method | |
# calculate lottery numbers | |
lottery_numbers = create_lottery_numbers() # calling method | |
# matched values | |
matched_numbers = user_numbers.intersection(lottery_numbers) | |
print("You matched {}. You won ${}".format(matched_numbers, 100 * len(matched_numbers))) | |
def get_player_numbers(): | |
number_csv = input("Enter unique 5 numbers from 1 to 20, separated by commas: ") | |
# converting to list of numbers | |
number_list = number_csv.split(',') # ['1', '2', '3', '4', '5'] | |
# creating a set of integers and converting into int using set comprehension | |
integer_set = {int(number) for number in number_list} | |
# Another method to create a set using map function | |
# integer_set = set(map(int, number_list)) | |
return integer_set | |
def create_lottery_numbers(): | |
values = set() # empty set | |
# If you are thinking to use for loop to generate numbers do it and find out | |
# why I am using while loop | |
while len(values) < 6: | |
values.add(random.randint(1, 20)) # using randint function to generate numbers | |
return values | |
menu() # start of app | |
A Lottery App using Python
Reviewed by Leaf Code
on
August 27, 2020
Rating:

No comments: