I'm learning Python in Udemy's super popular course ["100 Days of Code --The Complete Python Pro Bootcamp"] [4]!
I will summarize in Qiita mainly the places where I stumbled as the output of learning here: muscle:
Today I learned how to write an if statement, which is a conditional branch in Python!
Python3.8 VScode
It's annoying to talk about mess, so start with the code.
** ▼ Challenge ① **
number = int(input("Which number do you want to choose?"))
if number % 2 == 0:
print("This is an even number.")
else:
print("This is an odd number.")
In this code, you can enter any number you like and return "This is an even number." If the number is even number, and if it is odd number. I am trying to return "This is an odd number."
First, if if
is even number = that is, if you think that it is the same as dividing by ** 2 and the remainder becomes zero **, you can write number% 2 == 0
.
In addition, everything else is considered odd, so you can simply return "This is an odd number" without adding a conditional statement to else (other than that). Let's do it.
The point is, be careful not to forget ** this: (colon) ** when using if
and else
.
Learning from Ruby, I especially tend to forget this colon. (Fortunately, VScode uses a python plugin, so if you forget it, it will immediately tell you with a wavy line: smile :)
I also did something like this.
** ▼ Challenge ② **
#pizza order
print("Welcome to Python Pizza Deliveries!")
size = input("What size pizza do you want? S, M , L ?")
add_pepperoni = input("Do you want pepperoni? Y, N ?")
extra_cheese = input("So you want extra cheese? Y, N ?")
bill = 0
#code
if size == "S":
bill += 15
print("Small pizza: $15 ")
elif size == "M":
bill += 20
print("Medium pizza: $20 ")
elif size == "L":
bill += 25
print("Large pizza: $25 ")
if add_pepperoni == "Y":
if size == "S":
bill += 2
else:
bill += 3
if extra_cheese == "Y":
bill += 1
print(f"Your final bill is ${bill}. ")
It is a program that asks for the size of the pizza (S, M, L), asks if it has toppings, and tells you the final payment amount.
This is almost the same as before, except that elif
(abbreviation of else if) is used as a set with the if statement.
In the simple if and if else statements above, there is only one branch condition.
Then, when to use the elif statement, ** if you want to add a branch condition to it **.
Also, you can add as many elif
s as you like, so you can create unlimited branching conditions.
I won't explain the contents of the code in detail here, but it's interesting, so please try to decipher it and execute it!
I also did a little tricky branching condition task, so I will briefly introduce it.
** ▼ Issue ③ **
print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm?"))
bill = 0
if height >= 120:
print("You can ride the rollercoaster!")
age = int(input("What is your age?"))
if age < 12:
bill += 7
print("Child tickets are $7.")
elif age <= 18:
bill += 10
print("Youth tickets are $10.")
elif age >= 45 and age <= 55:
print("Everything is going to be okay. Have a free ride on us!")
else:
bill = 12
print("Adult tickets are $12.")
answer = input("Do you want a photo taken? $3 Y or N.")
if answer == "Y":
bill += 3
print(f"So your totall bill is ${bill}. ")
else:
print("Sorry, you have to grow taller before you can ride.")
It's a roller coaster problem. If you ask your height and it is 120 cm, the ticket price will change depending on your age. From there, ask if you want to add a photo option, and if you do, the final price will be the ticket price plus the photo price.
This task is easy if you calmly read and understand the else and elif statements enclosed in the if statement.
As the instructor said, the point when you don't understand the branching condition is to write ** logic tree ** by hand.
By doing so, you will have some idea of how to write it.
Then, today is like this!
I'm looking forward to tomorrow: smile :!
Recommended Posts