The lottery app - The final project

Python in a Day with 6 Projects Lottery - Try your luck
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.94
List Price:  €92.78
You save:  €27.83
£55.57
List Price:  £79.40
You save:  £23.82
CA$95.27
List Price:  CA$136.11
You save:  CA$40.83
A$107.88
List Price:  A$154.13
You save:  A$46.24
S$94.55
List Price:  S$135.08
You save:  S$40.52
HK$547.57
List Price:  HK$782.28
You save:  HK$234.71
CHF 63.42
List Price:  CHF 90.61
You save:  CHF 27.18
NOK kr759.63
List Price:  NOK kr1,085.23
You save:  NOK kr325.60
DKK kr484.38
List Price:  DKK kr692.01
You save:  DKK kr207.62
NZ$117.46
List Price:  NZ$167.80
You save:  NZ$50.34
د.إ257.02
List Price:  د.إ367.19
You save:  د.إ110.16
৳7,682.93
List Price:  ৳10,976.08
You save:  ৳3,293.15
₹5,837.41
List Price:  ₹8,339.52
You save:  ₹2,502.10
RM331.26
List Price:  RM473.25
You save:  RM141.99
₦99,285.71
List Price:  ₦141,842.81
You save:  ₦42,557.10
₨19,466.20
List Price:  ₨27,810.04
You save:  ₨8,343.84
฿2,553.28
List Price:  ฿3,647.70
You save:  ฿1,094.41
₺2,262.39
List Price:  ₺3,232.12
You save:  ₺969.73
B$349.43
List Price:  B$499.21
You save:  B$149.77
R1,335.92
List Price:  R1,908.54
You save:  R572.62
Лв126.45
List Price:  Лв180.65
You save:  Лв54.20
₩94,634.34
List Price:  ₩135,197.71
You save:  ₩40,563.37
₪258.03
List Price:  ₪368.63
You save:  ₪110.60
₱3,943.57
List Price:  ₱5,633.91
You save:  ₱1,690.34
¥10,600.67
List Price:  ¥15,144.47
You save:  ¥4,543.79
MX$1,161.53
List Price:  MX$1,659.40
You save:  MX$497.87
QR255
List Price:  QR364.31
You save:  QR109.30
P959.59
List Price:  P1,370.91
You save:  P411.31
KSh9,203.68
List Price:  KSh13,148.68
You save:  KSh3,945
E£3,310.52
List Price:  E£4,729.52
You save:  E£1,419
ብር3,976.27
List Price:  ብር5,680.63
You save:  ብር1,704.36
Kz58,526.41
List Price:  Kz83,612.74
You save:  Kz25,086.33
CLP$68,581.80
List Price:  CLP$97,978.20
You save:  CLP$29,396.40
CN¥506.04
List Price:  CN¥722.95
You save:  CN¥216.90
RD$4,144.87
List Price:  RD$5,921.50
You save:  RD$1,776.62
DA9,443.17
List Price:  DA13,490.83
You save:  DA4,047.65
FJ$158.28
List Price:  FJ$226.12
You save:  FJ$67.84
Q545.87
List Price:  Q779.86
You save:  Q233.98
GY$14,645.83
List Price:  GY$20,923.51
You save:  GY$6,277.68
ISK kr9,762.20
List Price:  ISK kr13,946.60
You save:  ISK kr4,184.40
DH709.20
List Price:  DH1,013.19
You save:  DH303.99
L1,234.28
List Price:  L1,763.34
You save:  L529.05
ден3,991.30
List Price:  ден5,702.11
You save:  ден1,710.80
MOP$564.10
List Price:  MOP$805.89
You save:  MOP$241.79
N$1,325.35
List Price:  N$1,893.44
You save:  N$568.09
C$2,576.69
List Price:  C$3,681.15
You save:  C$1,104.45
रु9,334.54
List Price:  रु13,335.63
You save:  रु4,001.09
S/259.58
List Price:  S/370.84
You save:  S/111.26
K267.89
List Price:  K382.72
You save:  K114.82
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,765.85
List Price:  ZK2,522.76
You save:  ZK756.90
L322.98
List Price:  L461.43
You save:  L138.44
Kč1,645.46
List Price:  Kč2,350.75
You save:  Kč705.29
Ft25,709.21
List Price:  Ft36,729.02
You save:  Ft11,019.81
SEK kr749.87
List Price:  SEK kr1,071.30
You save:  SEK kr321.42
ARS$60,034.20
List Price:  ARS$85,766.82
You save:  ARS$25,732.62
Bs483.70
List Price:  Bs691.04
You save:  Bs207.33
COP$271,296.95
List Price:  COP$387,583.68
You save:  COP$116,286.73
₡35,581.11
List Price:  ₡50,832.34
You save:  ₡15,251.22
L1,728.07
List Price:  L2,468.78
You save:  L740.71
₲516,441.87
List Price:  ₲737,805.73
You save:  ₲221,363.85
$U2,647.22
List Price:  $U3,781.90
You save:  $U1,134.68
zł280.50
List Price:  zł400.73
You save:  zł120.23
Already have an account? Log In

Transcript

Welcome back to the course, we will build a lot of application in this lesson. First, we go to File, and then create a new file. And then we are going to save that as a Python file. We can choose the desktop folder inside our own folder, we pick a name, say notary game, and we save it. Now we start to work on our pool Well first, import the random package. Next, we type in a global cash variable and assign that to be 2000.

This is the cash that we are going to spend for buying the lottery. Next, we are going to define a function to create a winning numbers. To do this, we use the D F, F and create underscore winning underscore numbers. Inside the function, we are going to create a local variable we name underscore numbers and divide that as a set. Now we use the while loop that we have learned before we do while length of the winning number set is less than six. we populate the set by using the f function of the set.

So we do winning numbers dot add and then use the Windham package. The when in function and define the range of the winning numbers to be one to 20 And then we do return meaning numbers. Now we save the Python file and then try to test it. We want to create winning numbers in our interactive shell and check the set. six elements has been generated in the set. Oh right.

Now, let's define the other functions. We do def, by on the school lottery and the school ticket. Inside this function, we are going to use the global cash variable. This is the cash that we are going to spend on the lottery ticket. We use the global keyword and then we defy cash equals to cash minus 50. So each time we buy a lottery ticket, we spend $50 After we bought the first ticket, the variable cash will be 9050.

However, there is a better way to write this assignment, we can use the shorthand minus equal 50. That will tell Python to minus 50 from the original cache variable and assign that back to the cash variable. Next, we are going to ask for user input for the lottery number that they are going to buy. So we use the variable enter numbers equals to input and then prompt them for six numbers. After that, we have to convert the user's input from a string to a list by using the split function that we have learned before. So we do enter number list equals to enter numbers.

Doc sprit And then we'll use the comma as the delimiter. After we have got the list, we now need to convert that to a set by doing ticket numbers equals to query bang cat for an in, enter numbers least. But don't forget that this n is now a string. So we need to use the INT function to convert that to an integer. Finally, we return the ticket numbers set. So the whole function is about first we minus the cash that we have by 50.

And then we ask for six numbers. After that, we split that up and make it into a list. And then we convert the list of numbers into a set of numbers in integer form. Finally, we return that back If we save the Python file, and then execute that in our interactive shell, we will see it is asking us for six numbers. So we put in 1357 910. And then immediately I have six numbers in our set.

And let's check how much cash we have. After buying the lottery tickets, we can see that we only have 1915 left. Okay, with no further delay, we are going to create the next function, which is to join the winning numbers and the Enter numbers. We are going to call this function join result and then put in two parameters. One is the ticket number set. The other is the winning number set.

First in this function, we are going to print out the winning number set. So we type in winning numbers, our query banquette and then format winning underscore lumbus. Next, we are going to create a variable which is called numbers match to find out the intersection between the winning numbers and the numbers that we enter. So, we use the intersection function have a set and pass in the winning number set. After that they what we have done before we are going to defy the price. So we do price equals to square banket.

Sudo Su Su 400. And then 2000 and then 40,000 and then 100 million This is going to be a list. Next we do prize equals two prices and then we put in length of the numbers match. So, the numbers match is going to give us a set of match numbers and then the length function is going to tell me how many numbers match. Based on this number. We can get the price Next we are going to do a check If price is bigger than zero, we are going to use the global keyword to assess the global variable cash.

And then we are going to say we add the price that we have earned to the cash that we own now we will use a shorthand here as well by plus equal and then we return a message. You match code banquette numbers and you just won Cody banquette. NTD for Taiwan dollars and to populate the curry banquette, we use the format function and then we say length of the numbers match this To tell us how many numbers we match, and then we put in price for the next number. Else we'll return better not next time. Finally, we are going to write our main function. So we do that one.

And then we use a while loop. This is going to be an infinite loop. So we do while true. And then inside a while loop, we have a memorable waiting for user to input. The palm will be welcome to notary store. And then we use a backslash n for a new line.

Next, we want to provide a few options for our user. First, is a buy option and then We provide a check balance option for them to check how much cash Do they still have And finally, we provide the option for them to get out of this application. Now, after this, we do a check, we say if command equals to the character B, then we do a pass elsif command equals to the characters See, we will play in cash elsif command equals to the character l will exit this infinite loop by using the command. Now we go back to the part for users to buy the lottery numbers. So we put in ticket numbers equal to buy lottery ticket and then this will go back and call our function buy lottery ticket, which will return a set of ticket numbers and store that in the ticket numbers variable.

Next, we define the variable winning numbers equals to query winning numbers. These will call our query winning numbers function. And we can see it will give us a set of winning numbers and that will be stored in the variable. Now we have to set and then we are going to assign the message variable To be the drawing result, we will pass in this to set as the parameters. This will call our join result function and then we will get a message and ripping them out. So after defining all our function, we call the one function and the one function we execute a while loop.

This loop will keep waiting for use the command until they choose leave. If the users choose to buy tickets, we'll call the buy lottery ticket function and then it will generate a winning number set. After that, we called a join result function. This will compare numbers that we buy with the winning number set. Now if use the choose to check balance, we will pin out the cash that is available. So let's save our Python file and execute that in our shell.

If we type See, we see we have 2000 cash available. If we choose to buy tickets, we have to input six numbers and it will generate a winning number set. And then it tells us well, Better luck next time. Let's try our luck again. We input sick numbers. This time we try 6011.

Unfortunately, don't match. So we type in C because we have bought two times. So we only have 1900 left. Finally, we type in Leave the application. So you can try yourself. If you are lucky and win the lottery prize, please share your good news with us.

So that's all about the lottery app. Let's take a break and we will see you in the next section.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.