Lucky Lottery Program In Python With Code
Last updated April 27, 2023 by Jarvis Silva
Looking for a way to create a lottery program in python then you are at the right place, today in this tutorial I will show you how to create a lucky lottery program using python programming so follow this tutorial till the end.
This python lottery program is the same as you taking a lottery and there are 5 numbers on it or you pick your 5 lucky numbers and then the result comes and if your numbers matches the result then you win a prize.
It is like a python lottery game, so to create this program I will use python loops without any libraries, I will show you everything from creating this lottery program in python and running it.
Python Code For Lottery Program
import random
lucky_numbers = []
user_numbers = []
correct_numbers = 0
print('Welcome To Lucky Lottery Numbers')
print('Enter 5 Numbers:')
# For generating random lucky numbers
for num in range(0,5):
random_num = random.randint(1, 100)
lucky_numbers.append(random_num)
# For getting user numbers
for num in range(0,5):
user_num = int(input())
user_numbers.append(user_num)
# For checking if got any lucky numbers
for lucky_num in lucky_numbers:
for user_num in user_numbers:
if user_num == lucky_num:
correct_numbers = correct_numbers + 1
print(f'You got {correct_numbers} correct numbers')
print(f'Result: {lucky_numbers}')
Above is the python lottery program. Now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer or else you can use this online python compiler to run it now.
To run this python program, follow the below steps:
- Create a new folder for this python project.
- Open it in a code editor of your choice.
- Create a python file with an ending .py extension.
- Copy the above code and paste it in your file.
- Now open a command prompt at folder location.
- And run this command: python filename.py
Follow the above steps to run this program. After running, it will ask you to enter 5 lucky numbers, then it will print the result of the lottery and show you how many numbers you got right. Below is an example output.
Output
Welcome To Lucky Lottery Numbers
Enter 5 Numbers:
6
23
4
32
21
You got 0 correct numbers
Result: [81, 34, 64, 7, 5]
Welcome To Lucky Lottery Numbers
Enter 5 Numbers:
64
32
4
23
2
You got 1 correct numbers
Result: [48, 78, 4, 73, 82]
As you can see, we successfully created the lucky lottery program in python. I hope you were able to run it successfully.
Python Lottery Program Explaination
You must have ran this program and saw that you enter 5 numbers and you get a result of five numbers and it tells you if you how many numbers you matched.
You may be wondering how does it do that so now let’s see how does this program work.
import random
lucky_numbers = []
user_numbers = []
correct_numbers = 0
Above are the first four lines of this program, On the first line we import the random module which I will tell you next, Then we declare two lists and a variable.
# For generating random lucky numbers
for num in range(0,5):
random_num = random.randint(1, 100)
lucky_numbers.append(random_num)
The random module we imported first was because to generate random numbers from 1 t 100 and store them in the lucky_numbers list using for loop.
# For getting user numbers
for num in range(0,5):
user_num = int(input())
user_numbers.append(user_num)
Above code is to take the user numbers we do the same thing here also we take the user input and store it in the user_numbers list using for loop.
# For checking if got any lucky numbers
for lucky_num in lucky_numbers:
for user_num in user_numbers:
if user_num == lucky_num:
correct_numbers = correct_numbers + 1
print(f'You got {correct_numbers} correct numbers')
print(f'Result: {lucky_numbers}')
Last part of the code is checking the result and the user entered number using for loops. We check if any number is equal to a lucky number if it is then we increament the correct_numbers variable.
Lastly we print the result along with the correct numbers the user got.
Summary
This was a simple lottery program in python. I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program.
Here are some more python programs for you:
I hope you found what you were looking for from this tutorial, and if you want more python guides and tutorials like this, do join our Telegram channel for future updates
Thanks for reading, have a nice day 🙂