164k views
2 votes
You are going to create a new function for creating a "list" of 8 lottery numbers

(again, you must do this with a custom FUNCTION following the requirements below. No function, no grade)
1. parameters
start value
end value
how many to choose (ie 8 numbers chosen out of a 1-60 number pool)
2. return
。 sorted list of numbers
3. Do not use: print(),input() inside the function, at all
4. Generating the numbers
use the random library (random.randint() or random.randrange()) use a loop
each time a number is generated, check to see if the number already exists in the list
so use the "if var in listname" type code
if number is in list, should continue back to top of loop to try again
if number is not in list, should .append() it to list
when you have completed the list
sort it
return sorted list
NOTE: While there is a function in the random library that would make this easier, so you can NOT use random.sample() nor random.choice(). You must use one of the two listed above (random.randint() is probably the best for this)
Using the Function
1. Generate the "winning" numbers, (choose 8 numbers out 60)
2. Generate a customers "lottery ticket" (also choosing 8 numbers out of 60)
3. Using whatever loops and list methods you feel appropriate, tell the customer how many, and which numbers they guessed correctly.
4. If they have 4 or more numbers correct, tell them they win a prize

1 Answer

2 votes

Final answer:

To generate lottery numbers, you can write a Python function using the random.randint() function, ensuring no duplicates by checking each generated number against the list. The numbers are then sorted and returned. Customers' and winning numbers are compared to determine the prize.

Step-by-step explanation:

Random Lottery Number Generator Function

To create a function in Python that generates a list of unique lottery numbers, you can use the random.randint() function from the random module. This function allows us to generate a random integer within a specified range.

The function will accept three parameters: start value, end value, and how many numbers to choose. It will return a sorted list of these numbers, ensuring no duplicates are present.

Step-by-Step Guide:

1. Import the random module.

2. Define the function with the appropriate parameters.

3. Within the function, initiate an empty list for the lottery numbers.

4. Use a loop to generate random numbers until the list contains the specified amount of unique numbers.

5. Within the loop, check if the generated number is already in the list.

6. If not, append it to the list; if it is, continue the loop without appending.

7. After the list is complete, sort it.

8. Return the sorted list of lottery numbers.

When using this function, you'll generate two lists: one for the winning numbers and one for the customer's numbers. Afterward, compare these lists to find out how many numbers the customer got right and whether they win a prize if they match four or more numbers.

User Mohammad Reza Mrg
by
7.8k points