Python Fruit Machine

Python Fruit Machine Average ratng: 9,8/10 5132 reviews
  1. Python Fruit Machine Game
  2. Python Fruit Machine Online

Python is a simple yet powerful programming language to learn. It accommodates all levels of programmers from beginners to advanced programmers. Python is flexible and can work on available operating systems e.g Mac, Windows and Linux. Have you learned about the bare basics of python but are confused on how to apply them? For example, a fruit may be classified as an orange if it’s round, about 8 cm in diameter, and is orange in color. With a naive Bayes classifier, each of these three features (shape, size, and color) contributes independently to the probability that this fruit is an orange.

In 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


Fruit machine code pythonBy 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


Python Fruit Machine Game


Other challenges you may enjoy...

Python Fruit Machine Online

Tagged with: iteration, List, Python Challenge