天天看點

python程式設計的條件語句_python程式設計 從入門到實踐 第五章 條件判斷語句

#char 5 if語句

#5.1 結構:if:

# sth to do

#else:

#sth to do

cars=["audi","bmw","subaru","toyota"]

for car in cars:

if car=="bmw":

print(car.upper())

else:

print(car.title())

#5.2條件測試

#5.2.1 檢查是否相等

car="bmw"

print(car=="bmw")

print(car=="audi")

#5.2.2 區分大小寫

car="BMW"

print(car=="bmw")

print(car.lower()=="bmw")

#5.2.3檢查是否相等

requested_topping="mushroom"

if requested_topping != "anchovies":

print("hold the anchovies")

#5.2.4 比較數字:

age=19

print(age==19)

#5.2.5檢查多個條件:

#采用and檢查多個條件

age_0=22

age_1=19

print(age_0>15 and age_1>15)

#采用or

print(age_0 >20 or age_1 >20)

#5.2.6 檢查特定值是否在清單中

requested_topping=["mushroom","onions","pineapple"]

print("mushroom"in requested_topping)

banned_users=["andrew","carolina","david"]

user="marie"

if user not in banned_users:

print(user.title()+" you can post a respon if you wish")

#5.3 if和 if_else語句

age=19

if age >=18:

print("you are old enough to vote")

age_0=15

if age_0 >= 18:

print("you are old enough to vote")

else:

print("sorry,you are too young to vote")

#5.3.3 is-elif-else結構:

age=12

if age <4:

print("your admission cost is 0")

elif age<18:

print("your admission cost is 5")

else:

print("your admission cost is 10")

#另一結構:

age=12

if age <4:

price=0

elif age <18:

price=5

else:

price=10

print("you admission cost is "+str(price))

#使用多個elif子產品

age=79

if age <4:

price=0

elif age <18:

price=5

elif age<65:

price=10

else:

price=5

print("you admission cost is "+str(price))

#省略else子產品,即可以通過所有的elif囊括所有的範圍值:

if age <4:

price=0

elif age <18:

price=5

elif age<65:

price=10

elif age>-65:

prince=5

print("you admission cost is "+str(price))

#測試多個條件:

requested_topping=["mushroom","extra cheese"]

if "mushroom" in requested_topping:

print("adding mushroom")

elif "pepperoni" in requested_topping:

print("adding pepperoni")

elif "extra cheese" in requested_topping:

print("adding extra")

print("\nfinish your ording")

requested_topping=["mushroom","extra cheese"]

if "mushroom" in requested_topping:

print("adding mushroom")

if "pepperoni" in requested_topping:

print("adding pepperoni")

if "extra cheese" in requested_topping:

print("adding extra")

print("\nfinish your ording")

#如果使用if-elif結果,當程式走到判定正确的位置,會跳出直接輸出結果,是以如果有多個判定條件,需要使用多個if語句

#5.4

requested_toppings=["mushroom","green peppers","extra cheese"]

for requested_topping in requested_toppings:

print("adding"+requested_topping+".")

print("\nfinish making the pizza")

requested_toppings=["mushroom","green peppers","extra cheese"]

for requested_topping in requested_toppings:

if requested_topping=="green peppers":

print("sorry,we are out of green peppers right now")

print("adding"+requested_topping+".")

print("\nfinish making the pizza")

#5.4.2确定清單是否為空

requested_toppings=[]

if requested_toppings:

for requested_topping in requested_toppings:

if requested_topping=="green peppers":

print("sorry,we are out of green peppers right now")

print("adding"+requested_topping+".")

print("\nfinish making the pizza")

else:

print("you are sure you want a pizza?")

#5.4.3 使用多個清單

avaliable_toppings=["mushroom","olives","green peppers",

"pepperoni","pineapple","extra cheese"]

requested_toppings=["mushroom","frensh fries","extra cheese"]

for requested_topping in requested_toppings:

if requested_topping in avaliable_toppings:

print("add "+requested_topping)

else:

print("sorry, wo do not have"+requested_topping)

print("\nfinish making pizza")

課後題

#5-1

#5-2

str_1="abc"

str_2="hijk"

str_3="efg"

print(len(str_1)==len(str_2))

print(len(str_1)==len(str_3))

str_4="ABC"

print(str_4.lower()==str_1)

a=24

b=13

c=46

print(a==b)

print(a>b)

print(a

print(a>=b)

print(a<=b)

print(len(str_1)==len(str_4) and len(str_1)==len(str_3))

print(len(str_1)==len(str_4) or len(str_1)==len(str_3))

numbers=[a,b,c]

print(numbers)

d=24

e=25

if d in numbers:

print("d is in the numbers")

if e not in numbers:

print("e is not in the numbers")

#5-3:

alien_color="green"

if alien_color == "green":

print(" you got 5 points")

if alien_color=="red":

print("you got 10 points")

#5-4

if alien_color=="green":

print("you got 5 points")

else:

print("you got 10 points")

if alien_color == "green":

print("you got 5 points")

if alien_color != "green":

print(" you got 10 points")

#5-5

if alien_color == "green":

print("you got 5 points")

elif alien_color ==" yellow":

print("you got 10 points")

elif alien_color=="red":

print("you got 15 points")

#5-6

age=22

if age<2:

stage="baby"

elif age<4:

stage="children"

elif age <13:

stage="teenager"

elif age<20:

stage="youth"

elif age <65:

stage="adul"

else:

stage="elder"

#5-7

favourite_fruit=["apple","orange","banana"]

if "orange" in favourite_fruit:

print("you really like banana")

if "apple" in favourite_fruit:

print("you really like banana")

if "banana" in favourite_fruit:

print("you really like banana")

if "pear" in favourite_fruit:

print("you really like banana")

if "peach" in favourite_fruit:

print("you really like banana")

#5-8

admin_list=["Mike","Sarah","Jack","Rose","Tom","admin"]

for name in admin_list:

if name == "admin":

print(name + " would you like to see some status report")

else:

print("Hello"+name+" , thanks for logging")

#5-9

del admin_list[0:6]

print(admin_list)

if admin_list:

for name in admin_list:

# if name == "admin":

# print(name + " would you like to see some status report")

print("Hello"+name+" , thanks for logging")

else:

print("we need to find some users")

#5-10:

current_user=["Mike","Sarah","Jack","Rose","Tom"]

new_users=["Mike","Sarah","Johnson","John","Kimi"]

for name in new_users:

for i in range(len(current_user)):

current_user[i]=current_user[i].lower()

if name.lower() in current_user:

print("you should set another name")

else:

print("Your account"s name is avaliable")

#5-11:

numbers=list(range(1,11))

for number in numbers:

if number==1:

print(str(number)+"st")

if number==2:

print(str(number)+"nd")

if number ==3:

print(str(number)+"rd")

else:

print(str(number)+"th")