python 第一行输入n表示一天中有多少人买水果_Python第五章

5.1 一个简单示例

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

for car in cars:

if car == ‘bmw‘:

print(car.upper())

else:

print(car.title())

5.2.1 检查是否相等

car = ‘bmw‘

if car == ‘bmw‘:

print("Yes!")

else:

print("No!")

car = ‘audi‘

if car == ‘bmw‘:

print("Yes!")

else:

print("No!")

5.2.2 检查是否相等时考虑大小写

car = ‘Audi‘

if car != ‘audi‘: # 2个大小写不同的值会被视为不相等

print("Yes!")

else:

print("No!")

car = ‘Audi‘

if car.lower() == ‘audi‘: # 判定并不会更改car中的值,car中还是‘Audi‘

print("Yes!")

else:

print("No!")

5.2.3 检查是否不相等

requested_topping = ‘mushrooms‘

if requested_topping != ‘anchovies‘:

print("Hold the anchovies!")

5.2.4 比较数字

age = 18

if age == 18:

print("Yes!")

answer = 17

if answer != 42:

print("That is not the correct answer. Please try again!")

age = 19

if age < 21:

print("Yes!")

else:

print("No!")

if age <= 21:

print("Yes!")

else:

print("No!")

if age > 21:

print("Yes!")

else:

print("No!")

if age >= 21:

print("Yes!")

else:

print("No!")

5.2.5 检查多个条件

1. 使用and检查多个条件(检查是否两个人都不小于21岁)

age_0 = 22

age_1 = 18

if (age_0 >= 21) and (age_1 >= 21):

print("Yes!")

else:

print("No!")

age_1 = 22

if (age_0 >= 21) and (age_1 >= 21):

print("Yes!")

else:

print("No!")

2. 使用or检查多个条件(只要至少1个条件满足,就能通过测试;2个测试都没有通过时,表达式结果为False)

age_0 = 22

age_1 = 18

if (age_0 >= 21) or (age_1 >= 21):

print("Yes!")

else:

print("No!")

age_0 = 18

age_1 = 18

if (age_0 >= 21) or (age_1 >= 21):

print("Yes!")

else:

print("No!")

5.2.6 检查特定值是否包含在列表中

requested_toppings = [‘mushrooms‘, ‘onions‘, ‘pineapple‘]

if ‘mushrooms‘ in requested_toppings:

print("True!")

if ‘pepperoni‘ in requested_toppings:

print("True!")

else:

print("False!")

5.2.7 检查特定值是否不包含在列表中

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

user = ‘marie‘

if user not in banned_users:

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

5.2.8 布尔表达式(结果要么为True,要么为False)

5-1 条件测试:预期 + 答案

car = ‘subaru‘

print("Is car == ‘subaru‘? I predict true.")

print(car == ‘subaru‘)

print("Is car == ‘audi‘? I predict false.")

print(car == ‘audi‘)

5-2 更多的条件测试:每个都来一个true和false

检查两个字符串是否相等

string_1 = ‘Big data‘

string_2 = ‘Big data‘

string_3 = ‘Big Data‘

print("string_1 == string_2:")

print(string_1 == string_2)

print("string_1 == string_3:")

print(string_1 == string_3)

使用函数lower() 的测试

print("string_1.lower() == string_3.lower()??

print(string_1.lower() == string_3.lower())

检查两个数字

age_1 = 66

age_2 = 68

print("age_1 == age_2:")

print(age_1 == age_2)

print("age_1 != age_2:")

print(age_1 != age_2)

print("age_1 > age_2:")

print(age_1 > age_2)

print("age_1 < age_2:")

print(age_1 < age_2)

print("age_1 >= age_2:")

print(age_1 >= age_2)

print("age_1 <= age_2:")

print(age_1 <= age_2)

使用关键字and 和or 的测试

print("False and True:")

print((age_1 == age_2) and (age_1 != age_2))

print("False or True:")

print((age_1 == age_2) or (age_1 != age_2))

检查特定值是否包含在列表中

requested_toppings = [‘mushrooms‘, ‘onions‘, ‘pineapple‘]

print("Is ‘mushrooms‘ in requested_toppings?")

print(‘mushrooms‘ in requested_toppings)

print("Is ‘ice cream‘ in requested_toppings?")

print(‘ice cream‘ in requested_toppings)

print("Is ‘mushrooms‘ not in requested_toppings?")

print(‘mushrooms‘ not in requested_toppings)

print("Is ‘ice cream‘ not in requested_toppings?")

print(‘ice cream‘ not in requested_toppings)

5.3.1 简单的if 语句

if conditional_test: # 如果测试通过了,将执 行if 语句后面所有缩进的代码行,否则将忽略它们。

do something

age = 23

if age >= 18:

print("You are old enough to vote!")

print("Have you registered to vote yet?")

5.3.2 if-else语句

age = 16

if age >= 18:

print("You are old enough to vote!")

print("Have you registered to vote yet?")

else:

print("Sorry, you are too young to vote.")

print("Please register to vote as soon as you turn 18!")

5.3.3 if-elif-else 结构

age = 13

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 = 13

if age < 4:

price = 0

elif age < 18:

price = 5

else:

price = 10

print("Your admission cost is $" + str(price) + ".")

5.3.4 使用多个elif 代码块

age = 13

if age < 4:

price = 0

elif age < 18:

price = 5

elif age < 65:

price = 10

else: # age >= 65

price = 5

print("Your admission cost is $" + str(price) + ".")

5.3.5 省略else 代码块

age = 13

if age < 4:

price = 0

elif age < 18:

price = 5

elif age < 65:

price = 10

elif age >= 65:

price = 5

print("Your admission cost is $" + str(price) + ".")

5.3.6 测试多个条件

如果顾客点了两种配料,就需要确保在其比萨中包含这些配料

requested_toppings = [‘mushrooms‘, ‘extra cheese‘]

if ‘mushrooms‘ in requested_toppings:

print("Adding mushrooms.")

if ‘pepperoni‘ in requested_toppings:

print("Adding pepperoni.")

if ‘extra cheese‘ in requested_toppings:

print("Adding extra cheese.")

print("\nFinished making your pizza.")

5.4.1 检查特殊元素

requested_toppings = [‘mushrooms‘, ‘green peppers‘, ‘extra cheese‘]

for requested_topping in requested_toppings:

print("Adding " + requested_topping + ".")

print("Finished making your pizza!")

requested_toppings = [‘mushrooms‘, ‘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.")

else:

print("Adding " + requested_topping + ".")

print("Finished making your pizza!")

5.4.2 确定列表不是空的

requested_toppings = []

if requested_toppings:

for requested_topping in requested_toppings:

print("Adding " + requested_topping + ".")

print("Finished making your pizza!")

else:

print("Are you sure you want a plain pizza?")

5.4.3 使用多个列表

available_toppings = [‘mushrooms‘, ‘olives‘, ‘green peppers‘, ‘pepperoni‘, ‘pineapple‘, ‘extra cheese‘]

requested_toppings = [‘mushrooms‘, ‘french fries‘, ‘extra cheese‘]

for requested_topping in requested_toppings:

if requested_topping in available_toppings:

print("Adding " + requested_topping + ".")

else:

print("Sorry, we don‘t have " + requested_topping + ".")

print("Finished making your pizza!")

5-3 外星人的颜色#1

alien_color = ‘green‘

if alien_color == ‘green‘:

print("You just earned 5 points!")

alien_color = ‘red‘

if alien_color == ‘green‘:

print("You just earned 5 points!")

5-4 外星人的颜色#2

alien_color = ‘green‘

if alien_color == ‘green‘:

print("You just earned 5 points!")

else:

print("You just earned 10 points!")

alien_color = ‘yellow‘

if alien_color == ‘green‘:

print("You just earned 5 points!")

else:

print("You just earned 10 points!")

5-5 外星人的颜色#3

alien_color = ‘red‘

if alien_color == ‘green‘:

print("You just earned 5 points!")

elif alien_color == ‘yellow‘:

print("You just earned 10 points!")

else:

print("You just earned 15 points!")

5-6 人生的不同阶段

age = 16

if age < 2:

print("You‘re a baby!")

elif age < 4:

print("You‘re a toddler!")

elif age < 13:

print("You‘re a kid!")

elif age < 20:

print("You‘re a teenager!")

elif age < 65:

print("You‘re an adult!")

else:

print("You‘re an elder!")

5-7 喜欢的水果

favorite_fruits = [‘apples‘, ‘oranges‘, ‘bananas‘]

if ‘apples‘ in favorite_fruits:

print("You really like apples!")

if ‘peaches‘ in favorite_fruits:

print("You really like peaches!")

if ‘bananas‘ in favorite_fruits:

print("You really like bananas!")

if ‘kiwis‘ in favorite_fruits:

print("You really like kiwis!")

if ‘blueberries‘ in favorite_fruits:

print("You really like blueberries!")

5-8 跟用户打招呼

user_names = [‘eric‘, ‘willie‘, ‘admin‘, ‘erin‘, ‘ever‘]

for username in user_names:

if username == ‘admin‘:

print("Hello admin, would you like to see a status report?")

else:

print("Hello " + username.title() + ", thank you for logging in again.")

5-9 处理没有用户的情形

user_names = []

if user_names:

for username in user_names:

if username == ‘admin‘:

print("Hello admin, would you like to see a status report?")

else:

print("Hello " + username.title() + ", thank you for logging in again.")

else:

print("We need to find some users!")

5-10 检查用户名

current_users = [‘eric‘, ‘willie‘, ‘admin‘, ‘erin‘, ‘Ever‘]

new_users = [‘sarah‘, ‘willie‘, ‘PHIL‘, ‘ever‘, ‘Iona‘]

current_users_lower = [user.lower() for user in current_users] # 等价于下面三行

current_users_lower = []

for user in current_users:

current_users_lower.append(user.lower())

for new_user in new_users:

if new_user.lower() in current_users_lower:

print("Sorry " + new_user + ", that name is taken.")

else:

print("Great, " + new_user + " is still available.")

5-11 序数:序数表示位置

numbers = list(range(1, 10))

for number in numbers:

if number == 1:

print("1st")

elif number == 2:

print("2nd")

elif number == 3:

print("3rd")

else:

print(str(number) + "th")

原文:https://www.cnblogs.com/XDZ-TopTan/p/13211986.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值