comparison operators
> greater than
< less than
>= greater than or equal to
<= less than or equal to
== equal to
!= not equal to
one equal sign(=) means that assigning this value to this variable.
two equal sign(==) means that checking to see if value on the left is equal to the value on the right.
Modulo
number = int(input("Which number do you want to check? "))
# The modulo is written as a percentage sign (%) in Python.
# It gives you the remainder after a division.
reminder = number % 2
if reminder == 1:
print("This is an odd number.")
else:
print("This is an even number.")
BMI Calculator 2.0
height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))
bmi = round(weight / height ** 2)
if bmi < 18.5:
print(f"Your BMI is {bmi}, you are underweight.")
elif bmi < 25:
print(f"Your BMI is {bmi}, you are normal weight.")
elif bmi < 30:
print(f"Your BMI is {bmi}, you are slightly overweight.")
elif bmi < 35:
print(f"Your BMI is {bmi}, you are obese.")
else:
print(f"Your BMI is {bmi}, you are clinically obese.")
Leap Year
year = int(input("Which year do you want to check? "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap year.")
else:
print("Not leap year.")
else:
print("Leap year.")
else:
print("Not leap year.")
Pizza Order Practice
print("Welcome to Python Pizza Deliveries!")
size = input("What size pizza do you want? S, M, or L ")
add_pepperoni = input("Do you want pepperoni? Y or N ")
extra_cheese = input("Do you want extra cheese? Y or N ")
bill = 0
if size == "S":
bill += 15
elif size == "M":
bill += 20
else:
bill += 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}")
logical operators
A and B
C or D
not E
Rollercoaster Ticket Box
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 = 5
print("Child tickets are $5.")
elif age <= 18:
bill = 7
print("Youth tickets are $7.")
elif age >= 45 and age <= 55:
print("Everything is going to be ok. Have a free ride on us!")
else:
bill = 12
print("Adult tickets are $12.")
wants_photo = input("Do you want a photo taken? Y or N. ")
if wants_photo == "Y":
# add $3 to their bill
bill += 3
# N 입력시 bill 추가하지 않으르모 else 구문 입력 필요없음
print(f"Your final bill is ${bill}")
else:
print("Sorry, you have to grow taller before you can ride.")
lower & count function
"Awesomee".lower() # “”속 글자를 소문자로 바꿔줌
# 'awesomee'
"Awesomee".count("a") # a는 str이므로 “a”로 입력해야함
# 0 : A와 a 별개로 인식함
lower_case_name = "Awesomee".lower()
lower_case_name.count("a")
# 1
Love Calculator
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
combined_string = name1 + name2
lower_name = combined_string.lower()
t = lower_name.count("t")
r = lower_name.count("r")
u = lower_name.count("u")
e = lower_name.count("e")
true = t + r + u + e
l = lower_name.count("l")
o = lower_name.count("o")
v = lower_name.count("v")
love = l + o + v + e
total = int(str(true) + str(love))
# str(true) + str(love) 은 str로 밑의 if 구문에 적용하기 위해 int로 변경 해줘야 함
if total < 10 or total > 90:
print(f"Your score is {total}, you go together like coke and mentos.")
elif total >= 40 and total <= 50:
print(f"Your score is {total}, you are alright together.")
else:
print(f"Your score is {total}.")
Treasure Island
# print('''
#
#''') print안에 여러 줄 입력할 때 사용 / 꼭 세개여야함
print('''
*******************************************************************************
| | | |
_________|________________.=""_;=.______________|_____________________|_______
| | ,-"_,="" `"=.| |
|___________________|__"=._o`"-._ `"=.______________|___________________
| `"=._o`"=._ _`"=._ |
_________|_____________________:=._o "=._."_.-="'"=.__________________|_______
| | __.--" , ; `"=._o." ,-"""-._ ". |
|___________________|_._" ,. .` ` `` , `"-._"-._ ". '__|___________________
| |o`"=._` , "` `; .". , "-._"-._; ; |
_________|___________| ;`-.o`"=._; ." ` '`."\` . "-._ /_______________|_______
| | |o; `"-.o`"=._`` '` " ,__.--o; |
|___________________|_| ; (#) `-.o `"=.`_.--"_o.-; ;___|___________________
____/______/______/___|o;._ " `".o|o_.--" ;o;____/______/______/____
/______/______/______/_"=._o--._ ; | ; ; ;/______/______/______/_
____/______/______/______/__"=._o--._ ;o|o; _._;o;____/______/______/____
/______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_
____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____
/______/______/______/______/______/______/______/______/______/______/_____ /
*******************************************************************************
''')
print("Welcome to Treasure Island.")
print("Your mission is to find the treasure.")
choice1 = input('You\'re at a cross road. Where do you want to go? Type "left" or "right" \n').lower()
# 바깥 따옴표를 ""로 입력하는 경우 안의 ""와 중복되므로 ''로 변경
# ' 가 apostrophe로 사용되는 경우에는 \'로 '앞에 백슬래쉬 입력하면 됨
# .lower() 로 입력값 대소문자 구분 없이 인식되도록 함
if choice1 == "left":
# continue in the game.
choice2 = input('You\'ve come to a lake. There is an island in the middle of the lake. Type "wait" to wait for a boat. Type "swim" to swim across. \n').lower()
if choice2 == "wait":
# continue in the game.
choice3 = input("You arrive at the island unharmed. There is a house with 3 doors. One red, one yellow and one blue. Which colour do you choose? \n").lower()
if choice3 == "red":
print("It's a room full of fire. Game Over.")
elif choice3 == "yellow":
print("You found the treasure! You Win!")
elif choice3 == "blue":
print("You enter a room of beasts. Game Over.")
else:
print("You chose a door that doesn't exist. Game Over.")
else:
print("You get attacked by an angry trout. Game Over.")
else:
print("You fell into a hole. Game Over.")