(Transcribed by TurboScribe.ai. Go Unlimited to remove this message.
A price should be a floating point number. For example, we might have dollars and
cents. We need a decimal.
So let's typecast our input as a float. Then a quantity. We will accept some user
input.
Our prompt will be, How many would you like? Quantities, they should be whole
numbers. Let's typecast our input as an integer. Then we will have a total.
What's the total that we have to pay? So let's take the price of each item. Use an
asterisk for multiply. Our quantity.
Then let's do a test run. Let's print our total. What item would you like to buy?
Let's say a pizza.
What is the price? $10.99. How many would you like? I would like five pizzas. And
our total is $54.95. Let's say that before we display the total, let's print the
following. I'll use an f-string.
You have bought, insert a placeholder, display our quantity, x, item, or items.
I'll add slash s. Then we will print, I'll use an f-string again. Your total is,
display our total.
What item would you like to buy? I would like to buy a pizza. What is the price?
$10.99. How many would you like? I would like nine pizzas. They're all for me.
I'm going to eat all of them. You have bought nine x pizzas. Your total is $98.91.
Let's add a unit of currency before this.
Pick unit of currency before displaying the total. I'll pick American dollars. I
would like to buy pizza.
What is the price? $9.99. How many would you like? I would like a dozen pizzas, 12.
You have bought 12 x pizzas. Your total is $119.88. All right, everybody.
That is how to accept user input in Python. And we've covered a few exercises. In
the next topic, we're going to create a Mad Libs game.
And that is how to accept user input in Python. All right, everybody. In this
video, we're going to create a game of Mad Libs.
Not because we have to, but because I want to. It would be a good exercise for us,
just so we're more comfortable with accepting user input. If you're not familiar
with Mad Libs, Mad Libs is a word game where you create a story by filling in the
blanks with random words.
So we're going to create a story template. The story is going to be missing some
components. We will fill in those components with random words that we type in.
Here's a story that I've written myself. Print, use an F string. Today, I went to
a, insert a placeholder, zoo.
For our placeholder, we'll insert an adjective. We'll insert a variable named
adjective. Adjective, adjective one.
You're going to get an English lesson today too. An adjective is a description of
something. So for our zoo, adjective one could be expensive, large, dirty.
An adjective describes something. We'll fill this in when we accept user input. For
our second print statement, let's print the following.
In an exhibit, I saw a placeholder. We'll include a noun, noun one. A noun is a
person, place, or thing in English.
Maybe a gorilla, a gorilla named Harambe, for example. Print, use an F string.
Let's say our noun one, whatever this is, we can reuse variables, was, we will
create a second adjective, adjective two.
We will be describing whatever noun one is, this person, place, or thing. And we
will insert a verb, verb one. A verb is an action such as running or eating.
Then for our last statement, let's print, I was at a placeholder. We'll create
adjective three. Adjective three will describe us.
Now we're going to fill in these variables by accepting user input. We're going to
fill in adjective one. We'll accept user input using the input function.
Enter an adjective. I'm going to add a reminder that an adjective is a description
of something. Then we need noun one.
Noun one equals input. Enter a noun. A noun is a person, place, or thing.
Then we have adjective two. I'll just copy adjective one, paste it, change one to
two. Then a verb.
Verb one equals input. Enter a verb. I want verb one to be in current tense.
I'll ask the user to end the verb with ing. Enter a verb ending with ing. Then it's
current tense.
Our person, place, or thing of noun one is currently doing something such as
eating. And then adjective three, and I'll just copy one of these adjectives.
Adjective three equals input.
Enter an adjective. Okay, and then we are ready to run this. Enter an adjective.
An adjective describes something. I will say suspicious. Or some kids like to say
sussy, or sus even.
I've also heard of kids nowadays using the word skibbity. Feel free to type in
whatever you would like. It is your story after all.
I'm gonna say suspicious. Enter a noun, a person, place, or thing. I like to poke
fun at Mark Zuckerberg, so I'm gonna say my person is Mark Zuckerberg.
Enter an adjective. That is a description. Angry.
Enter a verb ending with ing, so it's current tense. Screeching. Enter an
adjective.
Happy. Here's my story. Today I went to a suspicious zoo.
In an exhibit, I saw a Mark Zuckerberg. Mark Zuckerberg was angry and screeching. I
was happy.
That's our game of Mad Libs. It's a word game where you create a story by filling
in the blanks with random words. Also, post the output of your Mad Libs game in the
comment section down below because I really want to read them.
I want to see what you guys came up with. And well, everybody, that is a Mad Libs
game using Python. Hey, everybody.
In this video, I'm gonna show you all of the different math that we'll need
throughout the rest of the series. I have a lot to cover, and I'll split this video
into different sections. We'll cover some basic arithmetic operators, built-in math
functions, a few functions from the math module, and then a few exercises.
Be sure to look at the timestamps if you would like to skip ahead to another
section. Let's begin with some really easy stuff. We're going to cover some basic
arithmetic operators.
Let's say we have a variable, friends. Currently, you have zero friends. If you
need to increment a variable by one, you could say friends, the name of the
variable, equals the name of the variable again, plus one.
So the plus sign is the addition operator. And I think we do have a little bit of
experience with that already. So if I were to print my variable, friends, guess
what? You now have one friend.
We could also shorten this line of code. You could say friends plus equals one.
That would do the same thing.
This is known as an augmented assignment operator. That will give you the same
result. I prefer to use augmented assignment operators just because they take less
text.
And I think they're easier to read. Now let's use subtraction. Friends equals
friends minus two.
So of course, minus is the subtraction operator. You have negative two friends, I
guess. If you were to use the augmented assignment operator, that would be friends
minus equals two.
There, you still have negative two friends. Okay. Multiplication.
Let's change friends to, how about five? Friends equals friends times three. You
now have 15 friends. Then the augmented assignment operator version of this would
be friends times equals three.
So again, you have 15 friends. Let's cover division. Friends equals friends divided
by two.
So we have 2.5 friends. Somebody was cut in half. We have half a friend.
Maybe it's just their legs or torso or something. Then the augmented assignment
operator would be friends divided by equals two. And the result is still the same.
Now exponents. Friends equals friends to the power of two. So friends is currently
five.
Friends to the power of two would be five times five, which is 25. The augmented
assignment operator version of this equation would be friends exponent equals two.
And again, friends is 25.
Then we have modulus. Modulus gives you the remainder of any division. Suppose we
have 10 friends instead of five.
I will assign a new variable. Remainder. Remainder equals friends.
The percent sign is known as the modulus operator. It will give us the remainder of
any division. If I were to divide my group of friends by three, we will have one
remaining.
I'll store the remainder within a separate variable. We would have a remainder of
one. It's kind of like in class when the teacher says for everybody in the class to
go into groups of three.
Then there's always that one kid that's by themselves. That's kind of the same
concept. We're dividing our friends into groups of three.
Then the modulus will give you the remainder. If we divided our group of friends
into groups of two, well, 10 divides by two evenly. So there is no remainder.
So that is the modulus operator. It's fairly popular to use this operator to find
if a number is even or odd. Because it will divide by two evenly if that number is
even.
If the remainder is one, that means that the original number is odd. Okay, so yeah,
those are some basic arithmetic operators. Addition, subtraction, multiplication,
division, exponentiation, then modulus.
Now what we're going to do is cover some built-in math-related functions. Suppose
we have three variables. X equals 3.14. Y equals 4. Z equals 5. It doesn't matter
if these are floating point numbers or whole integers.
The first is the round function. We have a variable named result. I'm going to
round x. So there is a built-in round function.
After the set of parentheses, we can add some value or variable to be rounded. So
we will round x to the nearest whole integer, then print the result. So our result
is 3. So that's the round function.
With the absolute value function, we can find the absolute value of a number. Let's
change y to be negative 4 instead of 4. We'll take result equals abs, which means
absolute value of y. The absolute value is the distance away from 0 as a whole
number. The absolute value of negative 4 is 4. Okay, let's change y back to 4.
There's a built-in power function.
Result equals pow. Then we'll need a base and an exponent. What's y to the power of
3? That would be 4 times 4 times 4, which is 64.
That's the pow function. You can raise the base to a given power. The next two are
really useful.
Using the max function, we can find the maximum value of various values. What's the
maximum value between x, y, and z? Then I'll just need to store this value. Results
equals the max between x, y, and z. Well, the maximum value is 5. Otherwise,
there's min.
What's the minimum value between x, y, and z? That would be 3.14. In this next
section, we do have some very useful constants and functions from the math class,
but we'll need to import the math module at the top of our text editor. Import
math. If you need the value of pi, you'll type the name of the math module, dot pi.
I'm just going to print this. Print math dot pi. The value of pi is 3.14159 and a
bunch of digits that come after.
If you're working with physics, I do know that people use the constant e a lot. We
won't be using e in this video series, but if you ever need access to it, just type
math dot e, and that will give you e, which is 2.71 something, something,
something. I believe e is known as the exponential constant.
If you need the square root of a number, let's say results equals math dot sqrt. We
can place a variable or a value within the square root function. Let's say we have
x again.
x equals 9. What is the square root of x? Then I will print whatever the result is.
The square root of 9 is 3. That is the square root function. There's a ceiling
function.
Result equals math dot seal. Seal will always round a floating point number up.
Suppose x is 9.1. So 9.1 rounded up is 10.
Otherwise, there's floor, which will always round a number down. Result equals math
dot floor. Let's change x to 9.9. 9.9 rounded down is 9. Those are some useful math
functions.
Let's go over some exercises. Okay, this first exercise, we are going to calculate
the circumference of a circle. We'll need the help of the math module because
there's some good functions in there.
To calculate the circumference of a circle, the formula is 2 times pi times r.
Let's ask a user for a radius because that's what r is. We'll accept some user
input. Enter the radius of a circle.
We will typecast the input as a floating point number. To calculate the
circumference, again, the equation is 2 times pi. We can get that from the math
module times whatever the radius is.
And the user is going to type that in. Then we will print whatever the
circumference is. Print.
We'll use an f-string. The circumference is our variable circumference. Enter the
radius of a circle.
I'll enter 10. Actually, 10.5. The circumference is 65.97. If you want to round and
truncate some of these numbers, we can use the round function around circumference.
Then round to a given decimal place.
I'll round to two digits. Again, 10.5. Rounded is 65.97. You could add a unit of
measurement too. Let's say centimeters.
10.5 is 65.97 centimeters. All right. That is the first exercise.
For this next exercise, let's calculate the area of a circle. We'll import the math
module. We'll ask for a radius, much like before.
Radius equals input. Enter the radius of a circle. We'll cast our input as a
floating point number.
The equation for the area of a circle is pi times radius squared. We could easily
use the built-in power function to raise our radius to the power of two. Then we
will display the area.
Print. I'm using an f string. The area of the circle is our area to some unit of
measurement.
Let's say centimeters squared. Enter the radius of a circle. 10.5. The area of the
circle is 346.36, but I would like to round this number to two decimal places.
I'll use that round function. And I'll place area and the number of digits to round
to within this function. Let's try that again.
10.5. The area of the circle is 346.36 centimeters squared. That is the second
exercise. For this last program, we're going to find the hypotenuse of a right
triangle.
The formula to calculate the hypotenuse of a right-angled triangle is c equals the
square root of a squared plus b squared. We'll begin by importing the math module.
We'll ask the user for the lengths of side a and b. a equals input enter side a.
We'll cast the input as a floating point number.
We'll do the same thing with side b. b equals enter side b. Now this part's going
to be confusing. We'll calculate c. We'll need a squared plus b squared. We'll take
a to the power of two plus b to the power of two.
Then we'll need the square root of all of this, whatever the result is. I will
surround this equation with math dot square root. That should give us our answer.
Let's print, using an f-string, side c equals whatever c is. So enter side a,
three, side b will be four, side c is five. All right, everybody.
So that was everything related to some arithmetic operators and math-related
functions in Python. And in the next video, we're going to cover a few things
involving string formatting. Hey, everybody.
In this topic, I'm going to explain if statements. An if statement is used to do
some code only if some condition we set is true. Else, we could do something else.
It's a basic form of decision making. If it's true, we do something. If it's not
true, we don't do it.
Let's ask a user for their age. Age equals input enter your age. I will typecast
the input as an integer.
Depending on what the user's age is, we can do one of a few things. Let's pretend
that the user would like to sign up for a credit card. But in order to do so, their
age needs to be greater than or equal to 18.
Well, we can check that. To use an if statement, type if, then some condition. What
would we like to check? Let's check to see if the user's age is greater than or
equal to 18.
Then add a colon, then hit enter. Any code underneath the if statement should be
indented. Make sure to pay attention to that because that's easy to miss.
If the user's age is greater than or equal to 18, let's print you are now signed
up. If I were to run this code, I'll type in my age. I'll type 21, hit enter.
This statement is true. Therefore, we will execute any code found within the if
statement. You are now signed up.
What if this condition was not true? Let's say my age is 13. Well, nothing happens.
If the condition we check is instead false, we skip over this code.
If you need to take a different course of action, you could add an else statement.
If this is true, do this. Else, we can do something else.
Let's print a different message. You must be 18 plus to sign up. I'll type in my
age again.
I'll say that I'm 13, hit enter. You must be 18 plus to sign up. That's basically
an if statement.
Do some code only if some condition is true. Else, you can do something else
entirely. It's a basic form of decision making.
The else statement is kind of like a last resort. We can check more than one
condition before reaching the else statement. We can add an else if statement,
which we just shorten to e-l-i-f, meaning else if.
Else if, let's check if age is less than zero. Then we'll print a different
message. You haven't been born yet.
Now, if I run this code, I'll say that my age is negative one. This condition is
false. We skip this code.
This condition is true. Therefore, we will execute this code and we skip the else
statement. You haven't been born yet.
Let's add another else if statement. You can add as many else if statements as you
want. Let's check to see if somebody's age is greater than or equal to 100.
We'll print a different message. Let's print, you are too old to sign up. If I were
to say my age is 111 years old, well, it states you are now signed up.
The reason that we didn't reach this part of our else if statement, that's because
this condition is still technically true. You do need to pay attention to your
order of if and else if statements. If I want to be sure that nobody over 100 is
signing up, I should probably move this to the beginning.
If age is greater than or equal to 100, then else if age is greater than or equal
to 18, we'll do something else. And to your age, I am 111 years old. You are too
old to sign up.
So those are if statements. If some condition is true, do something. Else if, you
can check something else.
If no above conditions are true, you can do something else entirely. It's kind of
like the default. Here's another example.
We'll ask a user if they would like some food. Response equals input. Would you
like food? We'll have the user type in y for yes or n for no.
If our response, now to check to see if two values are equal, you would use double
equals. If the response is equal to y, then we will print have some food. The
doubles equals sign is the comparison operator.
It will check to see if two values are equal. You don't want one equals because
that's the assignment operator. Python, in this case, thinks we're attempting to
assign the character y to response.
So for comparisons, use double equals. Else we can print no food for you. So would
you like food? I'll type y. Have some food.
Let's try it again. I'll type no, n for no. No food for you.
Here's a third example. We'll have a user type in their name. Name equals input.
Enter your name. If our name is equal to an empty string, that means they didn't
type in anything. So let's yell at the user.
You did not type in your name. Else we will print, using an f string, hello,
whatever the name is. Enter your name.
I'm just going to hit enter. You did not type in your name. Let's run this again.
I'll type in my name. And we have executed the else statement this time. Hello,
bro.
So one important thing that you should know is the use of booleans with if
statements. Suppose we have some boolean variable named for sale. I'll set this to
be true.
Now using an if statement, you can just use the boolean variable in place of a
condition. Because a condition would evaluate to be true or false. We could just
say, if for sale, if that's true, then let's print, this item is for sale.
Else we will print, this item is not for sale. For sale is set to be true. This
item is for sale.
If this variable were false, well, then the item is not for sale. Let's try a
different variable. How about online? If online, the user is online.
Else the user is offline. So the user is offline. I'll change the boolean to true.
The user is online. So with if statements, you can either write a condition or you
could use a boolean. All right, everybody.
So those are if statements. Do some code only if some condition is true. Else you
can do something else.
(This file is longer than 30 minutes. Go Unlimited at TurboScribe.ai to transcribe
files up to 10 hours long.)