Lottery Numbers

lottery-numbersIn this challenge we are going to write a Python program that automatically generates six random numbers (from 1 to 50) and display them on the screen, sorted in ascending order.

The program will need to make sure that each number is unique; the same number cannot come twice in the selection of six selected numbers.

Learning Objectives


By completing this code, you will understand the difference between the two types of loops used in Python:

  • For Loops (Count-Controlled Loops): A count-controlled loop is one that is executed a certain number of times. In our example, because we know that we need exactly six numbers we are going to use a for loop to repeat (iterate through) some of our code exactly six times.
  • While Loops (Condition-Controlled Loops): A condition-controlled loop such as a While loop keeps going while a certain condition is true. As soon as the condition becomes false, the loop stops iterating. In our case, when generating a random number, we are going the check that the new number has not already been picked up. If it has then we are going to generate a new one and will carry on doing so till we get a number that has not already been picked up.

Check the code

Your Challenge

  1. Complete this code to store your own selection of six numbers. For testing purposes, you can store these numbers directly into the code and display these to the end-user. Alternatively you can ask the end-user to type six numbers.
  2. Then your code will use the program given above to generate the six lottery numbers.
  3. Finally your code will check and inform the end-user of how many numbers the user guessed correctly by comparing the user’s six numbers with the six randomly generated lottery numbers!

Note


The probability of guessing several lottery numbers is very low. You may want to tweak your program to only pick up numbers between 1 and 20 (instead of 50). This will give you a better chance to guess some of these numbers and hence test whether your code is working.

Video Tutorial



unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 18

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , ,