[udemy] python 부트캠프 _ section 3_ 흐름 제어와 논리 연산자

Dreamer ·2022년 8월 7일
0

1. if/else 및 조건 연산자를 통한 흐름 제어

  • if / else : 특정 조건에 따라 A 또는 B를 수행하는 것.
if condition:
  do this
else:
  do this
water_level = 50
if water_level >80 :
  print("Drain water")
else:
  print("Continue") 
print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm? "))

if height >=120: #120보다 크거나 같다면!
  print("You can ride the rollercoaster!")
else:
  print("Sorry, you have to grow taller before you can ride.")
  • height>120 인지 확인하는 조건문, if, else, 들여쓰기가 중요함!
  • left == right : left, right value가 같은지 확인.
  • left = right: left, right value가 같다.

2. quiz

  • 7 % 3 = 나머지 1을 출력함
  • 7 // 3 = 몫 2를 출력함.
number = int(input("Which number do you want to check? "))

if number % 2 == 0:
  print("This is an even number")
else :
  print("This is an odd number.")

3. 중첩 if 문과 elif 문 (Nested if /else)

if condition:
  if another condition:
     do this
  else:
    do this
else:
    do this

print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm? "))

if height >=120: 
  print("You can ride the rollercoaster!")
  age = int(input("What is your age?")
  if age < 12:
     print("Please pay $5.")
  elif age <= 18:
    print("Please pay $7.")
  else:
     print("Please pay $12.")
else:
  print("Sorry, you have to grow taller before you can ride.")

3. quiz _ bmi 2.0

height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))

bmi = weight/height**2
bmi = float("{:.2f}".format(bmi))

if bmi < 18.5 :
  print(f"Your BMI is {bmi}, you are slightly underweight.")
elif bmi < 25:
  print(f"Your BMI is {bmi}, you have a 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 obse.")
else:
  print(f"Your BMI is {bmi}, you are clinically obse.")
  

4. quiz _ 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")

5. 다중 연속 if문

if condition 1:
   do A
if condition 2:
   do B
if condition 3:
   do C
    

-모두 참이라면 세 가지가 모두 실행됨.

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.")
  else:
    bill = 12
    print("Adult tickets are $12.")
  
  wants_photo = input("Do you want a photo taken? Y or N. ")
  if wants_photo == "Y":
    bill += 3
  
  print(f"Your final bill is ${bill}")

else:
  print("Sorry, you have to grow taller before you can ride.")

6. quiz _ pizza

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 total pay amount is $ {bill}.")
    

7. 논리 연산자

if condition1 & condition2 & condition3:
   do this
else:
   do this
  • A and B = 둘 다 맞을 때 TRUE
  • A or B = 하나라도 맞을 때 TRUE
  • not E = E가 아닐 때 TRUE

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":
    bill += 3
  
  print(f"Your final bill is ${bill}")

else:
  print("Sorry, you have to grow taller before you can ride.")

8. quiz _ love calculator

print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")

name = (name1+ name2).lower()

t_count = name.count('t')
r_count= name.count('r') 
u_count= name.count('u')
e_count= name.count('e')

l_count= name.count('l')
o_count= name.count('o')
v_count= name.count('v')


total_front = str(t_count + r_count + u_count + e_count)
total_back = str(l_count + o_count + v_count + e_count)

total_score = int(total_front + total_back)


if t(otal_score <10) or (total_score>90):
  print(f"Your score is {total_score}, you go together like coke and mentos.")
elif 40 <= total_score <= 50 :
  print(f"Your score is {total_score}, you are alright together.")
else:
  print(f"Your score is {total_score}.")

9. quiz _ treasure island

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.") 

print("Welcome to Treasure Island.\n Your mission is to find the treasure.\n")
first_choice= input("You\'re at a cross road. Where do you want to go? Type \"left\" or \"right\" \n")
first_choice_as_lower = first_choice.lower()

if first_choice_as_lower == "left":
  second_choice = (input("you\'ve come to a lake. There is an island in the middle of the lake. Type \"wait\" to wait for a boad. Type \"swim\" to swim across.  \n")).lower()
  if second_choice == "wait":
    last_choice = (input("You arrive at the island unharmed. There is a house with 3 doors. One red, one yellow and one blue. Which colur do you choose? \n")).lower()
    if last_choice == "red":
      print("It\'s a room full of fire. Game Over.")
    elif last_choice == "blue":
      print("You enter a room of beasts. Game Over.")
    elif last_choice == "yellow":
      print("You found the treasure! You Win!")
    else:
      print("You chose a door that doesn't exist. Game Over.")
  else:
    print("You ge attacked by an angry trout. Game Over.") 
else:
  print("You fell into a hole. Game Over.")
  
  • 문자열 안에 " ", ' ' 둘 다 쓰려면 앞에 백슬래쉬() 붙여주기!!
  • input 뒤에 입력되는 데이터를 다음 줄에 입력 받으려면 input( " \n ") 형식으로 문자 안에 \n 써주기.
profile
To be a changer who can overturn world

0개의 댓글