Basics in Python With a Simple Lottery Number Game

Posted on by By Sohail, in Java, Javascript | 0

So today we going to cover few Basic knowledge of Python with the help of simple Game

The Game today is the Lottery Number Game

Make data easy with Helical Insight.
Helical Insight is world’s best open source business intelligence tool.

Click Here to Free Download

Game:

Each Player needs to decide how many numbers they want to play ranging from 1 number to 6 different numbers, and if you’re lucky one of the numbers entered can be your lucky number.

Solution:

The Idea of this game is to cover few Basic Concepts Python has to offer seamlessly. So we are going to cover the following:

  1. The Use of Modules
  2. Taking Inputs from the User
  3. Use of For Loops
  4. The Use of Functions
  5. The Use of Set

So let’s build the simple pieces of code in blocks and then piece them together

  1. The Use of Modules

We can use different Modules in Python with the use of the Keyword <caps>Import<caps>

So in our Case, we need to be able to generate random numbers to match with the user’s numbers. So we use the keyword <caps>Import<caps>. Hence we write

<code>import random<code>

          2. Taking Inputs from the User

With the Use of the keyword <caps>input<caps>, we can take inputs from the user

notoplay = input(‘Enter How Many Numbers you want to play [Range 1 – 6]: ‘)

  1. Use of Loops

For loops in other Languages are as follows:

for(i=0;i<n;i++)

now in python

we can define for loops as follows

for i in range(5):

<TAB>print(i+1)

<TAB> here implies this is within the for Loop or within the function code now the above python for loop can be translated to our normal syntax as a range is i<=n

       4. Use of Function

A function can be defined as follows

functionname():

<TAB>Start Code

<TAB> here implies this is within the for Loop or within the function code  once you define a function you can call it where ever you want with the use of just the functionname()

  1. Use of Set

Since we need to pick and compare two sets of different values i.e one the User inputted values and the other randomly generated values, then we might need to maintain uniqueness and set does that because A set contains an unordered collection of unique and immutable objects int_n_set = set() now we have defined a set we can add values to a set using .add()

Now we can code we this basic idea.

Now how do we compare the values from the user and the values randomly generated, we use the winnings = getplayernos.intersection(randomlotterynos) now with a simple if else condition we can tell how much the user won.

Make data easy with Helical Insight.
Helical Insight is world’s best open source business intelligence tool.

Get your 30 Days Trail Version

Now with this code, we can cover several use cases but here is a start, go ahead code your way, if any doubts, do comment

Sample Code:

import random

notoplay = input('Enter How Many Numbers you want to play [Range 1 - 6]: ')

maxplaynos = 6

if(int(notoplay) > int(maxplaynos)):

print('Enter A Valid Number')

notoplay = -1

def menu():

getplayernos = gpn()

randomlotterynos = cln()

winnings = getplayernos.intersection(randomlotterynos)

if(len(winnings)<= 0):

print('Better Luck Next Time')

if(len(winnings)> 0):

print('You Won {} '.format(100**len(winnings)))

print(randomlotterynos)

def gpn():

int_n_set = set()

for i in range(int(notoplay)):

j = i + 1

nos = int(input('Enter Lottery Number {}: '.format(j)))

int_n_set.add(int(nos))

return int_n_set

def cln():

rand_set = set()

nos = notoplay

while len(rand_set) < int(nos):

rand_set.add(random.randint(1,99))

return rand_set

menu()
logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

In case if you have any queries please get us at support@helicaltech.com

Thanks

Sohail Izebhijie

Helical IT Solutions Pvt Ltd

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments