Day 3 - Udemy - 100 Days of Python 学习笔记
·
Udemy - 100 Days of Code: The Complete Python Pro Bootcamp
Day 3 - Beginner - Control Flow and Logical Operators
22. If / Else and Comparison Operators
# If/else
if condition:
do A
else:
do B
# Comparison Operators 比较运算符
== # Equal to 等于
!= # Not equal to 不等于
> # Greater than 大于
< # Less than 小于
>= # Greater than or equal to 大于等于
<= # Less than or equal to 小于等于
23. Modulo
# Check Odd or Even
number_to_check = int(input("What is the number you want to check? "))
if number_to_check % 2 == 0:
print("Even")
else:
print("Odd")
24. Nested if statements and elif statements
# Nested if statements
if condition1:
do A
if condition2:
do B
if condition3:
do C
# If/elif/else
if condition1:
do A
elif condition2:
do B
else:
do C
25. Multiple if statements
# Multiple if statements
if condition1:
do A
if condition2:
do B
if condition3:
do C
26. Pizza Order Practice
print("Welcome to Python Pizza Deliveries!")
size = input("What size pizza do you want? S, M or L: ")
pepperoni = input("Do you want pepperoni on your pizza? Y or N: ")
extra_cheese = input("Do you want extra cheese? Y or N: ")
bill = 0
# todo: work out how much they need to pay based on their size choice.
if size == "S":
bill += 15
elif size == "M":
bill += 20
elif size == "L":
bill += 25
else:
print("You have chosen an invalid size.")
# todo: work out how much to add to their bill based on their pepperoni choice.
if pepperoni == "Y":
if size == "S":
bill += 2
else:
bill += 3
# todo: work out their final amount based on whether if they want extra cheese.
if extra_cheese == "Y":
bill += 1
print(f"Your final bill is: ${bill}.")
27. Logical Operators
# Logical Operators 逻辑运算符
A and B # Returns True if both statements are true 与
C or D # Returns True if one of the statements is true 或
not E # Returns True if the statement is false 非
28. Treasure Island Project
print("Welcome to Treasure Island.")
print("Your mission is to find the treasure.")
choice1 = input('You\'re at a crossroad, where do you want to go? '
'Type "left" or "right".\n').lower()
if choice1 == "left":
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":
choice3 = input("You arrive at the island unharmed. "
"There is 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 got attacked by an angry trout. Game Over.")
else:
print("You fell in to a hole. Game Over.")更多推荐


所有评论(0)