While loop - Getting the numbers from gamers

Python in a Day with 6 Projects Lottery - Try your luck
5 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.90
List Price:  €92.72
You save:  €27.82
£55.48
List Price:  £79.26
You save:  £23.78
CA$94.84
List Price:  CA$135.49
You save:  CA$40.65
A$107.44
List Price:  A$153.49
You save:  A$46.05
S$94.44
List Price:  S$134.92
You save:  S$40.48
HK$547.73
List Price:  HK$782.50
You save:  HK$234.77
CHF 63.11
List Price:  CHF 90.16
You save:  CHF 27.05
NOK kr759.51
List Price:  NOK kr1,085.06
You save:  NOK kr325.55
DKK kr484.31
List Price:  DKK kr691.90
You save:  DKK kr207.59
NZ$117.18
List Price:  NZ$167.41
You save:  NZ$50.23
د.إ257.02
List Price:  د.إ367.19
You save:  د.إ110.16
৳7,686.18
List Price:  ৳10,980.73
You save:  ৳3,294.54
₹5,834.28
List Price:  ₹8,335.04
You save:  ₹2,500.76
RM330.66
List Price:  RM472.40
You save:  RM141.73
₦97,668.24
List Price:  ₦139,532.04
You save:  ₦41,863.80
₨19,468.63
List Price:  ₨27,813.53
You save:  ₨8,344.89
฿2,546.70
List Price:  ฿3,638.31
You save:  ฿1,091.60
₺2,265.91
List Price:  ₺3,237.16
You save:  ₺971.24
B$351.06
List Price:  B$501.53
You save:  B$150.47
R1,322.01
List Price:  R1,888.66
You save:  R566.65
Лв126.52
List Price:  Лв180.76
You save:  Лв54.23
₩94,315.14
List Price:  ₩134,741.70
You save:  ₩40,426.55
₪257.38
List Price:  ₪367.71
You save:  ₪110.32
₱3,931.96
List Price:  ₱5,617.33
You save:  ₱1,685.37
¥10,592.26
List Price:  ¥15,132.45
You save:  ¥4,540.19
MX$1,160.67
List Price:  MX$1,658.17
You save:  MX$497.50
QR255.40
List Price:  QR364.88
You save:  QR109.47
P964.60
List Price:  P1,378.06
You save:  P413.46
KSh9,279.27
List Price:  KSh13,256.67
You save:  KSh3,977.40
E£3,320.75
List Price:  E£4,744.14
You save:  E£1,423.38
ብር3,980.93
List Price:  ብር5,687.29
You save:  ብር1,706.36
Kz58,401.19
List Price:  Kz83,433.85
You save:  Kz25,032.66
CLP$68,800.17
List Price:  CLP$98,290.17
You save:  CLP$29,490
CN¥505.43
List Price:  CN¥722.08
You save:  CN¥216.64
RD$4,147
List Price:  RD$5,924.54
You save:  RD$1,777.54
DA9,435.57
List Price:  DA13,479.97
You save:  DA4,044.39
FJ$157.96
List Price:  FJ$225.66
You save:  FJ$67.70
Q546.07
List Price:  Q780.13
You save:  Q234.06
GY$14,663.88
List Price:  GY$20,949.30
You save:  GY$6,285.42
ISK kr9,758.70
List Price:  ISK kr13,941.60
You save:  ISK kr4,182.90
DH709.58
List Price:  DH1,013.74
You save:  DH304.15
L1,235.01
List Price:  L1,764.37
You save:  L529.36
ден3,991.30
List Price:  ден5,702.11
You save:  ден1,710.80
MOP$564.38
List Price:  MOP$806.29
You save:  MOP$241.91
N$1,336.41
List Price:  N$1,909.24
You save:  N$572.83
C$2,577.43
List Price:  C$3,682.20
You save:  C$1,104.77
रु9,342.84
List Price:  रु13,347.49
You save:  रु4,004.64
S/260.45
List Price:  S/372.08
You save:  S/111.63
K268
List Price:  K382.87
You save:  K114.87
SAR262.59
List Price:  SAR375.15
You save:  SAR112.55
ZK1,736.79
List Price:  ZK2,481.24
You save:  ZK744.44
L322.64
List Price:  L460.94
You save:  L138.29
Kč1,642.49
List Price:  Kč2,346.52
You save:  Kč704.02
Ft25,571.54
List Price:  Ft36,532.34
You save:  Ft10,960.80
SEK kr748.30
List Price:  SEK kr1,069.05
You save:  SEK kr320.74
ARS$60,059.22
List Price:  ARS$85,802.56
You save:  ARS$25,743.34
Bs483.92
List Price:  Bs691.35
You save:  Bs207.42
COP$270,819.36
List Price:  COP$386,901.39
You save:  COP$116,082.02
₡35,222.95
List Price:  ₡50,320.66
You save:  ₡15,097.70
L1,728.74
List Price:  L2,469.73
You save:  L740.99
₲517,758.37
List Price:  ₲739,686.52
You save:  ₲221,928.15
$U2,629.19
List Price:  $U3,756.16
You save:  $U1,126.96
zł279.29
List Price:  zł399
You save:  zł119.71
Already have an account? Log In

Transcript

Welcome back to the course, this session, we are mainly going to talk about the while loop. While loop is very similar to for loop, but it's more flexible. For example, if we define a variable C and D to be an empty string, and then we use the while loop to check if C and D lot equals to quit. Then we start to do now everything in the loop is going to be executed by Python. So here we defy CMD equals to the input function. And then the poem is enter command.

And let's see what happened next. At the very beginning, Python is going to chat what is the variable CMD Because this anti shame, so it can get into the loop. And then it will wait for the user input. As long as the user input is not equal to quit to do you keep wanting and because the wearable CMD is initialized as an empty string, that's why Python is waiting for us to enter the command. Suppose if we put okay in after one in the input function, Python will go back to the while loop condition and check whether the C empty variable equals to the string quit because they are not the same. So you see Python will ask you for the command again, until we put in quit now to do is expected to leave is accepted because the last time we put in the string quit and that Python code back to the wild condition and check because this some CMD variable really equals to the string quit.

So that's why today cannot be continued. And there is something called infinite loop. If we do something like while true, this condition will always be satisfied. here inside the loop, let's define the variable C and D to be an input function of enter command again. And then if CMD equals to the Christian, we will break. So let's see what happened.

This way have defined to do is actually exactly the same as the way before. So firstly, Python will check the while loop condition. Originally it is true, so we enter the loop and then it will print now enter command and wait for user input. If the user input is equal to the string Then it will break. Otherwise, it will keep looping until we type in quit. Because when we type in quit CMD equals to quit will be satisfied, and then the program will break.

Now let's take a look at how we can apply the while loop into the lottery numbers generation. First, we import the package random. And then we declare the variable lottery underscore numbers to be assessed, and I will use the while loop and check if the length of the lottery numbers is less than six. This is to say if these no two number set has less than six animals We will execute the things in the loop, which is lottery numbers, dot add. And then we'll use the random function random dot when int. And then we put in one, and 20.

Now if you take a look at the result, and print out lottery underscore numbers, you see we have six elements. These numbers are from one to 20. Let's take a look at this while loop again. First a month, we import the random package. And then we set the lottery numbers to be an empty set. And then in the while loop, we try to populate this empty set one by one, as long as the set has less than six elements and the number that we add is the most random integer between one and 20 this is generated by the random package that we have import.

After the element is added, it will go back to the checking condition of the while loop. As long as we have less than six elements in the set, it will keep adding new number to the set at a time where we have six elements in the set the loop will be executed because the checking condition does not satisfy this one at the end we have six numbers in a set and they are not repeated alright. So, that is what we have learned about how to use the while loop. In the next section we are going to talk about the split function. See you next time

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.