Transcripts
1. Course Preview: Hey guys, I'm La DemullI'm,
a software developer. I'm going to be a
Toto for this tutor simulating a roulette
game in Python. Trust Tutoria is
going to be found. Firstly, we're going to be
explaining how class works. How to declare class method attribute, class
level attributes, and how to use every
together without an error. After that, we're going
to dive deep into writing the simulating program itself
by using the class method. We're going to be using
class attribute method and class level
attribute in tutoria. After that we're
going to be using try and accept to save our
program from crashing trust. We're going to learn a lot in, I don't want you to miss from
all the tricks and tips I'm going to be sharing with us for the delay.
Let's get started.
2. Introduction to classes: Hey guys, welcome
back to my storia of simulating a roulette
game in Python. In this tutoria, we're
going to explain what is class method attribute, class, class level
attribute to stove. We are going to explain
it in this storia. First of all, I want
us to understand that everything in Python
is an object. Either string topo
dictionary set, other stop are objects and
they have their own method, they have their own
properties that they use. Let me show you
something about that. Let me open my terminal Af typing Python to use the
keywords and Pythons, I'm going to create a
variable variable name cage, a random age and numbers
list of random number. Now let's check the type
of these variables. All I have to do is to put in type and the name
of the variable. I consider that this
variable name is using the type string that
this variable is. The instant is the object of class string and
has the ability to use all the properties or
attributes of the class string. Let me check the type of
the gels type integers. That means the attributes, the variable of H
is the instinct is the object of class
integer and has the ability also to
use all the properties and attributes and all the
methods of integer string. Also, let me check the type
of the variable number. That is the name object is based on the class
of class string. The object is based
on the class integer. That is, that means I
can use the method, I can use the attribute, I can use all the method
in the class string. For example, name, upper upper case is a
method in the object. In the class string, you change the variable name. The jewel in side,
it's a capulet. Also, you want to use
the method of the list. Inside the list, we
have something like a pen that just add
to the list that numbers a paint inside it. I'm going to pass in the
value I want to A pain. Okay. It's not showing. So
let me call it out numbers. Well, it has added new
element of the list, ten into the list. We can see that inside we
can use all the attributes, the method of a
particular class. What do we mean by class? What do we mean by a class? A class is the blueprint
of how object look. It is not the object itself, but it describe an object. It describe an object, how all the object
should look and behaves. Inside the class, we
have the function called the Se function. The function is a function that absolve or receive
all the attributes, all the attributes of the class, attribute of the class. We can also say these
function runs when we create a new instinct of the class or objects based
on the class object. Like the former example, when we create the
variable name, Variable name is an object
of the class St, so it runs. The function runs to activate all the attribute of the string, function, of the string of the string class and
it's also a method. Under standards are
all the function created under a class. Functions created
under a class are methods, methods,
methods, parameter, parameter, parameter self
refers to the instinct of the class we are creating self. Inside this parameter self refers to the instinct
of the object. We're going to get, we're going to understand
more about this. As we moved on, we use
class to create an object. The self refers to
the object we are creating that is
more explanatory. Also inside this
innate function, we have what we call tribute. Just tribute can be anything, tank of it, like a, an attribute color mode, the products name
attribute of the class. All those ones are attributes of the class and
can be assessed and can be input when an object is called
relating to the class. Also inside it we have what we call a
class level attribute. A class level attribute is like a unique or universal
attribute of the class. Think of it like a planet. All the planets We know
about a brand shape, shape equals brand is a class level attribute
for all the planet, for all planet, for
all the planets. Let's dive more deep into this. Think of these like a car, have a car inside. In this function we have
self and all attributes. All the attribute we have, name, maybe a Toyota car. Legos, another attribute model, maybe the 2020 to 2022 color. Maybe the color
is red or yellow, or gold in color inside
it. You have to method. Remember, I told
you other time that all the functions inside
a class is a method. It has a method, start engine, stop
engine, or kill engine. Those are methods
inside the class. Those are method
inside the class. And it has a class level
attribute that is a tire that means all the cars we know
as a and ship the tire is D. Let's dive deep into the by
writing a code, a program. Think true writing program is going to be more
understandable. We're going to understand
is more better. I'm going to open a text
editor, presently using Tony. I'm not going to use it
in the rest of the code. I want to use this. Explain what I mean by
class and the thing. We are going to
create a name planet. The planet is going
to have attribute that is the name of the class, radius, the gravity,
and the system. We're going to create an
object that is going to assess all this attribute and we're going to
create a method, also class attribute, starting class and the
name of the class planet. Starting with a
capitulator side, we have a function,
double underscore in. Double underscore is
going to be the string, there is going to be the
attribute of the class side. We are going to
have set parameter. We are going to assign all
the other attributes into the S parameters dot name equal to name of the
planet, less the planet. String the radius of the planet. Let's take a random radius. Let's say 201,000 maybe
centimeter or miles or kilometer. Anyway, so gravity wants to know the position of
the Earth where the stay, the gravity, let's say the
gravity is 5.4 0.4 How system the system the planet is
operating on is operating on planet system, system. We've declared all
our attributes num and we've assigned it to the safe parameter inside it said earlier and
explain about safe. Now we're going to declare an object that
is going to make use. Of all these attributes
inside the class. All these attributes
inside the class. Let's say a user with
somebody I want to use. I want to declare the values, values of the name of the
Earth, radius, gravity. It's going to be user equal to. We're going to call the
name of the planet. Name of the planet. I've said my program
with the name planet Y. We can now use the attribute
of the class planet. But let's guest Planet. Name of planet. Planet. So it's going to be a format
string name of planet. We are not going to be
using self dot name again. We have a object now the state
that we can use to assess the attribute of the class because we have
already assigned it to plant class planet. It's going to be user name, user name joined to just
copy to make it faster. I'm the radius, radius. To get the radius the planet radio is. This is going to
print out the name and the radius of the planet. We see the name of planet,
planet, planet, radius. Anything we can put here maybe storm that when we run into, it's going to show
that historic. You can see the planet's
reader historic. Now the user can use all the attributes including
the methods of the class. For now, we have
already declared all the values the attributed. What if we want to input the name of the
planet ourselves? We don't want to have unique name of the Earth.
How are we going to do that? We are going to declare the
attributes inside this place, so it's going to be radius. We're going to declare them as a parameter inside
the function in it, gravity and system. So we come here, it's no
more going to be held, it's going to be named, just follow along radius gravity system that we are assigning
all the attributes here into this self by using
the same saved radio, saved, everything can be inside the self so that it can be
accessible by this user. Now, for us to input the
name of our own planet, instead of having a Uniq, we come here and input all
the values from this place. We are not going to
input this one is like a Python parameter itself. The value we want
the name to be, the value we want
the radius to be, the value we want
the gravity to be, is going to be input inside
the bracket of planet. Inside the bracket of planet, let's say the name is mass. Mass and radius is 30,000
miles or anything and the gravity is 2.5 0.22 0.5 system is planet. X system. If we rerun, this one must give us exactly. We are the one that
inputs the values we want for this particular planet. This is how to input values we want for
your own attributes. The name of your own attributes, the numbers of your
own attributes. Remember, when you're declaring, don't declare this
one as a function. Remember to put
it in the so that the program not appear input, you're putting a variable
instead of name. Now we want to declare a method. A method that we make this
planet to orbit, this planet. To orbit, we're going to declare a method by
declare another function. Remember I told you
the other time. So we're going to
call the name of the function Spin
Spin Spin function as zone, attribute as self. We are going to print out or return informative string or print out informative string. Let's print print out return, let return informative
string self, that is the name
of the planet is orbiting in, that is this planet. The name of the planet,
maybe Earth or anything is orbiting in the system system, the system of that
particular planet, planet. So for us to assess this one, you have to come down here and print that is the attribute dot, dot spins. Remember when we want
to use the attribute, dots are pain for the list we did the other
time Inside our power shell, we use dot to access
that particular method. To use the method
we have to put, we can now access this
zone from our code. Let's run this and
Sm is going to run, See mass is orbiting
the planet X system. The name of the system we put, the name of the system
we put and the name of the planet. We
can use it here. Let's declare a level attributes that is like in general
or yeah, in general. Attributes of the planet that no matter how
the planet is, they have a
particular attribute. Let's say ship equal to, we know all planets
are around the ship. We can also, you can access this
one from this please. For us to access that one
we have to use something. Print user ship. This one we give us the ship. We don't have to
put the parentheses there because it's not a method. Is a class level attribute, is not a method that is going to maybe accept anything before. This is not a function, is like a variable declared inside the class and
is not a variable, is a class level attribute, declares fo for us to use this, you don't need to
put any apprentices in front of you if
you run this one. Now it's going to tell us
the shape of the Rand's, tell the shape of the planet. And you can see it's return run. This is how to declare a class. From declaring a class, to declare the
attribute of the class, and coming down to
the claim method and inputting your own the name of your own planet or anything. And assessing it and assessing the method,
assessing the ship. With this, I think you guys understand more
better about class. When we launch into the
simulating program, we are not going
to get confused. So we're going to work together and when I'm
talking we're going to understand this was a
literary or basics many of class so that
we can understand and move together with
me in the next studio. When we are going
to be writing or diving deep into
the program itself. When we are going to be writing all the attributes of the game
and how the game is going to work together and
give us our answers. See you guys in the next studio.
3. Writting the Roulette Simulating program: Hey guys, welcome
back to this Storia. In this storia,
we're going to be writing the four
Python programming, simulating the roulette game. And we're going to
declare our class, the attribute we're
going to use, and the methods,
and each loop we're going to be using
in this program. First of all, before
we get started, I put on this picture to
explain how this game works. This game is popularly played
in a casino, a casino. The roulette, well, this one, the roulette wheel
is composed of alternating red and black slot, each slot with his own number. Prior to spinning the wheel, this wheel is to be spin. This we is to be
spin we that contain red and black slot which is specific number
is to be spin. Before this wheel can be spin, a player plays a bet
on the roulet table. This table, they start
with a fixed amount of money and bet a small
part of this amount. Each spin, the player that is going to play this
particular game will bet a particular
amount of money. Maybe it's going to start
with $100 and each spin, maybe it's going to be being
maybe $500 at each spin. The main objective of the
player is to guess what number the roulette wheel will land on when they play the
bet on this table. Maybe the plays
on 15, number 15. The main objective
of the player is that when the wheel rules, the wheel is going to stop at number 15 in order to
win the most money. When the spin and stop at number the number
that the player bet, the player is going to gain
a lot of money out of it. When the player has
lost all their money, they are out of the game. In Tutoria, we're
going to be writing a program that we
create a player. The player, we choose
a specific number to play when spin and roll. If the player gets
the right number, is going to win a lot of
money in digit, not cash. No, we are not talking
about cash in digit. The players spin and be bet, bet and he continues
to lose and lose. All his money's going
to be out of the game. So we are going to
simulate the player bet on a single number 0-49
In this program, we're not going to be using
this type of number there. We are going to be
writing a program that we represent the wheel to spin 0-49 Why they choose
their number? They also choose the
amount of money. The amount of money to play, when the player choose
a specific number, is going to choose the amount of money is going
to be used to be. In this game, the script generates a random number
in the range zero to 49. Simulating the rulet if the output number is the same
to the number of the wheel, if the number bet is the same
as the number of the wheel, the player is going to get 50 times the bet
if the player bet $1,000 If the number bet and the number the wheel
stop at correlate together, the player is going to
gain $50,000 to his money. If not, the player loses their bet and can try
again in the next round. If the player is
running out of money, the script stop and display
that they have lost. There be a minimum amount of
money that must be inside the player's account before
they can go for the game. If the player amount is lesser than the minimum amount that was
supposed to be the, the script will stop and print out a result that
the player lost. This is what we are going to be writing Python code about. Also, when the player
bet and amount of money we have to take to ensure this correspond with the
amount of money the player have left when the player bet
a specific amount of money, if the money the player bet is greater than
his total balance, the total amount of money
inside his account. The script is going to plat out an error message and the
program is going to stop. And we're going to take maybe the number the player
choose between, the range of 0.49 This is what we are
going to be writing, our program all about. I hope is fun enough.
Let's get started. I've opened my
visual studio code and I've saved my
program for late. You can save it to anything, but I prefer Sibley Fine name, that means you are using Python. Let's create our class. Let's create our classes. Name is going to start
with a capulets, going to have column
in front of it. Inside it, we are going to have our init function that is going to comprise all our
attributes of the game, double underscore,
double underscore. We won't be the want to input all our values
inside if you follow my last explanation before this one we want
to put attribute. Part of the attribute we want to put there is the user name. The name of the, the user amount that the amount the user
is going to start with. Let's say user balance,
the number picked. That means the number
the user is going to pick on the roulette table, the amount pot the user is
going to play for each round. That means if the user $5,000 user balance amount is going to, is going to separate the user
and the banner so that we can know what I put the and after that part
of our parameter. So we want to put
the number that the, the, the specific number the wheel is going to
generate after rolling. The number they will generate is going to be in
this parameter. Also, we want to
have a bonus rate. If the players guess right, the rate at which we are
going to multiply is be, let's name it, put
it as a bonus rate. The guess range that
is 0-49 the lastoria. As we said in the last toria, we're going to assign all
these attribute to this safe. Just to save time, I assign all the attribute to
the safe parameter. You compose the video
and do that right away. Also, after this, we are
going to declare our method. The method that is going
to spin the wheel, as we said in the last, that every function declared
under a class is a method. We're going to declare
method that we spin the wheel to generate a number. I'm going to name it Spin inside is going to have his
own safe parameters inside. I'm going to print
a message to inform the user to wait for 8 seconds. After that, I'm going to put something that
I'm going to explain. After this, I'm going
to set a timer, the time to sleep for 8 seconds. Text editor is telling
me that time is not defined because I've now
import it inside my program. For you to use this
time function, let me call it a function,
you must import it. So I will come here and
say import import time. So you can see that
the error messages have disappeared after that. For me to assess the values, the attributes
inside this class, inside this method,
inside this method, from another method,
I have to use this paramus I want to assess, I want to assess this
method from another method. I want to assess this method. Now, outside these two method, maybe when I declare the
object of the class, I don't need to put
the save there. I can only put the
name of the objects to assess the attribute
in the class, I hope on the other
standard one. Yeah, save will number because that's where we want
to see the name of the number of the
will it's going to be and it's going to be land. The rand mean random integers. We are going to generate
a random integer from zero to gas range. You consist to tell me
that have er messaged. That means random integer is
not divine because I have no import a modu value. The method inside have
this function inside. I have to import the
random random modu. Import random from random. I want to import
all the module of the random inside the pro graph. Import all. It's telling me that guest range is not defined. Why? Why is it
telling me this one? I'm going to explain
this later litter. Let's ride on a
little bit inside. It's going to return inside, inside this number,
a specific number that is going to return the number
generated from this place. As an answer, it's going to return save number. When we call this function, a number generated from this place is going
to be returned. Yeah, the variable
we call this method from is going to print out the number we
generated from this place. Basically, this guess range is fretting out a
narrow message. Because we have not defined
a guess range though, we've declared it inside
this as an attribute. We need to define it so that this method also can
assess it is not like this other one
that is going to assess like a safe number. But we need a specific
number so that the the program can know that is between zero to
this specific number. That's when this class
level attribute comes in. This is where we are going to declare all our class
level attribute. That means a unique value
that is going to represent all these values as
default, our user name. We're going to declare a class level attribute for
it, usable speak number. So that if though the user did not input
anything into it, this default value will be
the input for the program. Let's do that. User name, we declare all our class level attribute. We declare our user name
as an empty string. Our user balance as zero. Damage, the user
balance is zero. As default, the number
picked is zero. As default, the amount
played is zero. A default, I put
our bonus as 50. Remembering how these games work and now we are
going to be said, when the user gets
the right number, we're going to multiply
is the amount bet by 50. Our bonus rate is going to be 50 and guest range we
said is going to be 0-49 It's going to be from this place for me to
assess this guest range, this guest range in this place, I have to put the
name of the class. Do this guest range because
it's not under this function. It's now under this
function, under this method. If it's under this method, I can put save guest range. But it's not under this method, it's under the class itself. For me to assess it, I have
to put let guest range. Yeah, we see we don't have
any Ero message again, because we have put the range at which this game, the
guest range will be. So it's going to be 0-0 to 49, as I put it this way. So that if we change
the value there, we are not going to be
changing it all everywhere. We declared guess range in
our program where we change, it's going to change
globally. On. The program. That's
why we declare a class attribute after declare all the method we
need for this program. For now, we are going
to declare the object. That means the instinct that is going to assess all
the method in our program. We are going to
declare a variable. Players represent the player
that has to play the game. And he is going to have
all the attribute, all these attributes inside. It's going to be player in the last storia
equal to the name of the class bracket. We want to be want to input
all our values, our sales. We are going to be
using the name, the values of our
class level attribute. Now, explain this
in the last bot, best time to explain it, we can use the class
level attribute as inputs and change
it in our code. Again, we can use the
class level attributes, all our class attribute and later change it in later change. Just follow along. We're
going to understand this more better for us to use the value as we did so that we use
roulette guess range. That's what we're going to use. So we are not going to
be using same because if you use the program is
going to print out that we are using the attribute name. Again, we're declaring a
variable and a variable. Again, it's going
to be like a clash in the program for us now to use all the class level attribute
we are going to be of input or the values that we are going to
use as attribute. All these attributes
now can be used by this player. We get it. All these attributes inside this function can be
assessed by the player. Now due to what we create, they all have assigned all the class level
attribute by using the late username
reserve the balance, everything to the guess range so that we can use
it in our program. First of all, what do
we want the user to do after he or she
opened the game? We are going to
collect the name of the user and collect
the balance. That means the amount the user brings to the game
or to the party. Now for us to use
the attributes, now we have to be using rule
let dot so that the program, we know that we are referring to this particular class level
attributes, not this one. Again, for us to use
down to use user name, we want the user to
input a zone name. It's himself can
use name or digits. So we are going to use the
input function, your name. This is going to
collect the name of the user and it's going to
print a formatting string, lo, the name of the user, to greet the user the game. After that, we are going to the amount the player
brought to the game. The total amount the
player brought to the game user balance equal to the player is
going to be the one to input it in
into their format. After that, the name of the user and
the balance of the user, we are going to declare
a loop that's going to look constantly when the wheel rose and the
player wants to play again, the program have to run again. We don't want the player to
be running the game itself. We want it to be like
an interactive game. So that if the player
want to continue with the games going to input
a specific number, and if the user wants
to end the game, it's going to input
another specific number. Now we are going to declare
a y loop equal true. That's going to look forever, loop, loop and loop,
loop inside it. We are going to check. If you
remember in our questions, when I'm introducing the game, we declare that there must be
a specific amount of money. Maybe I do not remember,
there must be a se, amount of money that
the user must input. The total money we
want the user to use less than $100 $100
So we are going to declare that rule user balance is less than a Dred. To print a message for the user, it's going to bring out
message for the user that it can only bet $100 above. Anything less than that is
not acceptable in the game. It's going to break the loop. It's going to break the loop. After that, if the user inputs the right amount
of money for the game, the loop is going to continue. The loop is going to
print out the money. The input, the program is going to
print it out for the user. Know that this is a specific amount he's
using for the game. It's got to print
out a format string, It's got to print out
the balance of the user. After that, we are
going to allow the user to choose a specific number you
want to choose from the range of zero to 49. We are going to store that
inside the number picked, it's going to be an integer. We're going to input
a format string telling the user to
enter guess number 02. Now I don't want to put only 49. Maybe later in the game we
want to update the game. That number from 49
is now 0500 to 100. And I will now be
changing it anywhere I find the 49 instead of just coming here
and change it to 100 to save myself
of the stress. Next time I'm going
to update the game. So I'm just going to put the roulette guess
number guess range. Anytime I update the guest
range from maybe 49, 18, this one also is
going to be updated. The number selected
is going to store inside a number pick variable for the next
process after this, we're going to check if
the number picked from the user is inside
this range of number. We are going to declare if
statement that if late number picked is greater than guess range. Again, if the user pick a
number out of this range, maybe the user input
15 instead of 49 as his guest guess number the program is
going to print out. You can only print,
you can only pick number between zero
and the guest rest. Nine, continue lines of code
is going to be run again. This line of code is
going to be run again for the user to amend
his error or error. So after that, we're going
to move on to the next part. That is the user is going
to input the amount of money is going to
play for this round, like maybe this is
the first round, the amount of money the user is going to
use for this run, it's going to be
an integer input. Also, we're going to print a message that
enter the amount, sorry, you want to bet for this round, the amount input by the
user is going to start inside this variable,
amount played. Now we want to check
if the user input is less than the minimum
requirement of the game. The minimum requirement of the game is that
the user only be $100 and above. Anything less on $100 is
not allowed in the game, so you can only be $100
and above continue. This continues for this look to run again so that the
user can amend his. After that we're going to be the amount the user input is, maybe the amount the
user input is greater, is greater than
the total balance. Maybe the player input $2,000 A and it balance is $1,000 so that the
error can be corrected. So we are going to
use if statement. Again, the amount played is greater than usablealance's going to create a
narrow message. The amount is higher than your total money to correct
any mistake of the user. After that, the user
passed through all the, after the user passed
through the protocol, all these rules and
everything is correct. The wheel n is now
going to roll. Remember we want to
assess another method. We're going to use the
name of the object player, the name of the method, the name of the method
we want to use, the spin, Spin first spin. There's no parameter for
us to input to the spin. This method is going
to spin the wheel of the roulette game and
it's going to return, he's going to print
out the number for us, it's going to be a
formative string. Again, we stop, he's going to print out
the number where the wheel stop we run because we have saved this return value
inside this variable. We can use the, we can
assess the results. The spin method is
going to print for us from this variable, that's why I print it out here
as rule will number for us to know maybe the win number correspond to the
number the user input. We're going to declare
another loop that we check the two numbers, if the number is correct
is corresponded, is going to multiply the user
input by the bonus rate. We're going to a loop now. It's not going to be a white rule that is
going to run forever, it's just going to be
a simple statement. It's going to be roulette
number picked equal, equal to rule we
number here we are, saying that if these two
numbers are equal, do this. He's going to print out a congratulation message,
informal format string. So he's going to print
out congratulation. Now we want to print
out the amount the user is going to have
after winning the bet. It's going to be in form of calculation is going to be in. That's why we declare
format string. We are going to perform a simple calculation
here that means roulette bonus rate times, we're going to multiply it by
the amount the user played. If the user played, $100 is
going to be multiplied by the bonus rate that we declared
earlier in this place. That's 50 if the user input and a one day debate is
going to multiply by 50, dia means the user won $5,000 as bonus and it's
going to be added to balance plus equals
the rate times the amounts played after being calculated is going
to be added to the balance. Maybe the users hundred $1,000 balance in the game
and he played out 100. Dollar out of it
remain $100 out of it. And we debate as $100 times
50 is going to be $5,000 Dams $5,000 plus $1,000 is going to be the
balance of the user. That is going to
be $6,000 balance. And we are going to
break this year. We're going to
break this year as break if the guess is wrong. Another why that means roulette number picked
is not equal to, maybe is not equal to
the roulette number. He's going to print out another message to
prompt the user. And he's going to
subtract the amount played by the user
from the user balance. If the user we debate
the bonus times, the amount played is
going to be added to the balance and if the
user loses his bed is the amounts please minus we are going
to subtract it from the user balance
Brulet user balance. And we are going to
break this y loop here. After this, we are
going to print out the balance of the total
balance of the user. I'm now using a
formatting string. The amounts that remain
between either of these two will be printed out, will be printed out. After we are going to, if the user balance is still
satisfy this condition, we are going to check maybe the user balance
is still more or less than $100 If it's less
than $100 script yeah, we are going to use if statement
that roulette a balance less or equal to zero or Usabalance is less than Dred. Or we can even do this if let Usabalance is less than drain. Do this, print out
most of Shen fund, we are going to print game over. Remember very well, when I'm explaining how the
game is going to work, we say the user balance is
less than the expectation or less minimum fund or
she need for the game, the script to end. That's what we do. We have done that. We are
going to break this loop. This breakdown is
not working for this is not working
for this loop, is working for this loop. If we don't break the loop
is going to continue to run. And cost error, if the
user balance is still more than $100 the
user still have a chance to decide maybe to continue or to get
away with this cash. We are going to print out in formative string for the users, do you wish to continue with you? So we're going to prepare
the user balance. Yeah, we're going to put a
statement that's going to be like the answer from the user. This one is not going to
be a form of rueleds, going to be a answer because we declare it is not part of the
attribute of the game, a predefined variable
inside the code. It's going to be an integer that say input that we
received. The input of the. And tell the user
if yes input one. Sorry, if no, let me put this one, sorry. Let me put this on single, single, if not input. And you're going to pray that
if answer is equal to one, the look should start again. If answer is equal to zero, print string. Thanks for coming. This is going to end
the game for us. And if the user inputs anything
else is going to print not in the options
and it to break the lookting the program from declare will
attribute to method and collecting values
from the user. Now let's test our program. Maybe it is running
appropriate and check. Maybe there's any
day that we can fix. Let me run the program. The program is a for my name. As you said earlier,
I put my name, Eli's To put the total amount of money I want to
use for the game. Let me put something less
than the minimum requirement. Put 50 instead of order's
telling me that the, the money is lower
than expectation. That you can only be
dollar and above. And the program ends. Now I test only one part of the program and it's
working perfectly. Let me test it again. Let me put my name,
put $1,000 that I want to use for my
game and the balance $1,000 Enter the guess number between zero and for
now I want to check, maybe ever input anything
less or greater than 49. The program is
going to accept it. Let me put 50. It's split out. An error messy that you can only pick number 0-49 perfect, and it's split out by balance. Again, we put continue year, remember we put continue. That means this program
should run again. If there's any error, it runs again and asks me
to put a correct value. Let me put 20 as my guess. It asks me to put the
amount of money out of this $1,000 as my bet. Let me put something higher
than my total amount. Let me put something like
1,000 It's telling me that the amount entered is higher
than your total money. Perfect. That means my
code is running perfectly. It runs again, that as she
starts from picking my number, let me pick 15, now let me put $200 as my bet. He's telling me to wait.
Remember, in this method that I put is running
this method now, and he's telling me
that you were wrong. I input the wrong value. So he says, do we stopped
at 29? You're wrong. So it has removed my, the amount I bet from
my total balance, $1,000 And he's fretting out the total
balance now as 800. So it's not akimate. Maybe I want to continue with
my $800 So if I said yes, I want to continue, so he's got to ask for the number
I want to use again, 30, the amount I
want to bet this. Now let me say 500. Operate. Works out. Oh, I missed it again. Because see, our program
is running perfectly. We test it, but it
remains one more thing. What the amount I
input is correct. How is it going to calculate my balance and call
maybe I'm correct, I was going to add it to
my balance in this place. We are going to, in this case, we're going to reduce the
guess rate so that we can get our value. I'm going to clear my console, so I'm going to run it again. Jewel, my name, the amount
I want to claim it, I should guess 0-1 you say? Yeah. When I change this 1-1 it's changed automatically
Everything in my program. I don't go back to my program, I'll be changing everything 9-1111 because I've already declared a class
level attribute, like a universal
attribute for it. If I put in one, he's asked me for the
amount I want to use to be, let me say I lose my money. So let me start again and put
zero now and put hundred. So let's wait for the, wow, congratulations you have $500
that it's firstly print out where the will stopped and multiply amount I
play $100 times 50, that means the bonus
rate and it's print out and add it to my balance is Now he's asking
me maybe as opt out our just opt out it because see, our program is
running perfectly. The calculation, the algorithm
is working perfectly. What if the user
inputs something different from the code?
Let me run it again. Instead of the user
input in digits. The user, my program
is getting crushed. Instead of the user
input in digits, Put it, put it in string. What is happening?
What am I going to do? This is what we're
going to discuss in the lasturia, that is Nestoria. How we are going to
use, try and act, to resolve this arrow
from Oreo coin. That means if I run this program again and I put my name
and I put the amount, and instead of
putting 01 I put one, my program gets crushed again. This is what we are going
to resolve in the Nestoria. See you guys in the
Nestoria where we're going to finalize this
program. Thank you.
4. Conclusion: using try and except: Hey guys, come back to the
final part of this tutoria. In this part of the Studia, we're going to be
finalizing our code. I want to save it from crashing. You remember in the last
example, in the last story, I put on putting the
string instead of integer. And you see the arrow
that is showing a value. That means the value
we input inside the variable is wrong. Instead of putting integer, we put in string
for us to resolve. For us to resolve this,
we are going to be using the trancet tricept, simply declaring in the giving or passes from instruction
to the program. When thesis inputting
some values is no specific inputting
value can use from times. Also here we're
going to be using this for our input value when the user input a string instead of integer
or integer string. Do this instead of the
opgramstead of the programs to crash which just make it a simple instruction that the
code is also going to run. Again, we're going to go back to our code and come back to, the user is going to input
values, especially here. You're going to put
in the tri accept. Yeah, first of all, I want us to know when you
want to use the accept. Make sure that you
know the value, the type of error
you want to deal with sometimes
might not be value, might be attribute error. For us, here we are
facing a value error. We're going to
resolve this by using the tri and accept in our code. We're going to use try, at least try this line of code. And except except
inside this Pt, you're going to put the type
of error we are facing. I'm going to just copy this means we are
facing the value error. We are telling the
problem that you try the zone and there's a
type of value error. Instead of crushing the program, just print out invalid. So it's going to be a string. So when the program
runs and we input the wrong value's going
to put invalid. And it's for the
user to understand, well, we're going to put when the program runs, when the code drones and find a specific error is
going to print out this. Also. Let's come
back to our code. In the first part we have
to resolve this one. Also, if the user input
string gear or something, maybe add something like ash or dollar to the dollar
sign to the input. We want the program to print out an error that the program Des users input only in digit
we're going to put also except value error is mainly value we are
going to be solving. Maybe when you're still solving
some part of your code, you find some type of error. Maybe index error. Attributttributero. Sometimes it may
be from your abut. Mainly here we're just
going to be solving the value an error
codes were to print. Invalid input, we
have resolved this. Now when the program runs and we input the rock val is
going to execute this one. And if there's o is going
to stop the program here. But inside inside this loop, we know that the program
is going to be running constantly and we
don't want to stop it. We're going to put a condition
here under we have to put in this one
also so that it can run every to get there. I'm going to move this one, I'm going to put, so I get to, I'm going
to put continue. The continue year is different from the
continuum we put here. The continue year is saying that when the code is correct, you should move to the part of the code is not
talking about the loop. You can see the in
the Ind is different. In this continue is
talking about the y lo. If this rule is violated
and it's printers, Erin, this program
should run again. User can also amend
er inside the code. There's another part also
that we need to put a set. Let's put it to where
the user is going to input the values in this place. Let's put it that ever user input string instead
of integers. So we're going to use everything is going to be inside it. Remember the continue
is different from the other part of the continuum that I'm
going to show you. Now let me put the
type of error going to be value, the value. The program is to print
an error message. The program is going to
put an error message. We are going to tell it to continue to execute
this type of code. Again, we just put it in
the three different parts. And the other please? This other please. I didn't put it here because in my code I've
already instructed the program that if the user
input anything apart from this to put not in the
option this, please. Let me just run it
again to be sure. So I'm going to put my name. I did not put
anything here because sometimes it's a casino game. Some people might
not want to use anonymous name. So
they are covered. The might want to use
something like as one, they are covered inside the
displays to input my money. Maybe I input like
1,000 The program is going to tell me
that invalid input enter in digit symbol. I can see that there's an error and my
program is covered. My program didn't crash and
my program run perfectly. Jewel where to put in thousand. It's welcome me now let
me put 1,000 1,000 Yeah. This one is showing between
range 1.0 because I've not yet changed it back from 49, it's showing me to pick
from 10.1 If I put in one, it's telling me that please input digits and it's
executed line again. Now I know that my error is that I've input the wrong value. Let me input 11. And he's asked me the
amount I want to use. I put something like 100, 100 telling that invalid
input in entering digit. If we put something
like you don't input symbol there also
he's going to print it out. The user is going to
know that there's an error with water input here. You have to choose
again, the user is going to know that maybe
I shut symbol here again. The code is running
perfectly and that's the outcome. I missed it. We can see our code
is running perfectly. Now we've successfully viewed or simulate a roulette
game in Python. Let me just run
through it again. We declare our class, declare the tribute function. Remember this one is
double dark score. The net, the attribute
of the game method, maybe part of our
assessment is going to be we put all other, maybe the input name, the input amount,
everything is going to be a metal that we're
just going to be call lane under our code. They are not going to be
writing it like this. After that, we declare our
class level attributes. All our class level attributes. And we input our attributes, we assign it to this player. And we put in all the conditions
that the program was passed and execute for
the success of the game. Thank you very much
for watching this, to see you guys next time. Thank you.