python编程实践第5讲答案_Python编程入门到实践 - 笔记( 5 章)

第 5 章练习了以下内容

简单的 if 判断语句

判断字符串是否相等,还是不等

进行数字的大小比较

and,or 比较

检查列表中是否存在指定的元素

if,if-else,if-elif-else 语句写法

if 判断列表是否为空

使用多个列表进行比较判断

这一章的内容也比较简单,感觉和 shell 差不多,但还是多练习吧。

希望路过的大牛指出不足,小弟在此谢过了。

一个简单的 if 判断语句

循环打印 cars 列表中的元素,如果其中的元素等于 bmw,就全部大写打印

否则只是将元素的首字母大写

-------------------------------------------------

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

for car in cars:

if car == 'bmw':

print(car.upper())

else:

print(car.title())

-----------------------------------------------------

Audi

BMW

Subaru

Toyota

判断是否相等

大小写不一样,也会不等

--------------------------

car = 'Audi'

print(car == 'Audi')

--------------------------

True

--------------------------

car = 'Audi'

print(car == 'audi')

---------------------------

False

转换大小写进行比较

------------------------------------

car = 'Audi'

print(car.lower() == 'audi')

------------------------------------

True

检查是否不相等

不过两个值不相等,就打印

-----------------------------------------------

requested_topping = 'mushrooms'

if requested_topping != 'anchovies':

print("Hold the anchovies!")

------------------------------------------------

Hold the anchovies!

数字的比较

----------------------

age = 18

print(age == 18)

----------------------

True

进行 if 语句判断,如果两个数字不等,就打印

-----------------------------------------------------------------------------

answer = 17

if answer != 18:

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

-----------------------------------------------------------------------------

That is not the correct answer. Please try again!

不光可以进行比较是否相等,还可以比较大小

直接在 python 的 IDE 下进行比较是不是看着更方便。哈哈哈

>>> age = 19

>>> age < 21

True

>>> age <= 21

True

>>> age > 21

False

>>> age >= 21

False

检查多个条件

and 当两个条件都满足的情况下,打印 True 否则打印 False

>>> age_0 = 22

>>> age_1 = 18

>>> age_0 >= 21 and age_1 >=21

False

>>> age_1 = 22

>>> age_0 >= 21 and age_1 >=21

True

or 至少满足一个条件,打印 True 否则打印 False

>>> age_0 = 22

>>> age_1 = 18

>>> age_0 >= 21 or age_1 >= 21

True

>>> age_0 = 18

>>> age_0 >= 21 or age_1 >= 21

False

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

----------------------------------------------------------------------------

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

print('mushrooms' in requested_toppings)

print('pepperoni' in requested_toppings)

----------------------------------------------------------------------------

True

False

给要查找的值指定一个变量并查找,如果不存就打印出来

----------------------------------------------------------------------------

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

user = 'marie'

if user not in banned_users:

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

----------------------------------------------------------------------------

Marie, you can post a response if you wish.

简单的 if 语句

设定年龄为19,进行 if 语句判断,如果大于18就打印

---------------------------------------------------

age = 19

if age >= 18:

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

----------------------------------------------------

You are old enough to vote!

if-else 语句

设定年龄为 17,与 18 进行比较,如果大于等于 18 就打印 if 下的语句,

否则打印 else 中的语句

----------------------------------------------------------------------------

age = 17

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

------------------------------------------------------------------------------

Sorry, you are too young to vote.

Please register to vote as soon as you turn 18!

if-elif-else 语句

年龄设定为 17,分别进行判断,小于 4 多少钱,小于 18 多少钱,其他多少钱进行打印

--------------------------------------------------

age = 17

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

----------------------------------------------------

Your admission cost is $5.

简化一下上面的写法,将判断的值定义一个变量,最后打印

----------------------------------------------------------------

age = 12

if age < 4:

price = 0

elif age < 18:

price = 5

else:

price = 10

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

-----------------------------------------------------------------

Your admission cost is $5.

使用多个 elif 代码块

判断条件当然不止3个,这时候就用到了多个 elif 代码块

python 并不要求必须有 else 代码块,虽然书里是这么写的,但我作为小白的我还是倔强的认为这个习惯不太好,

所以自作主张不练这个 ’省略代码块‘ 了

----------------------------------------------------------------

age = 12

if age < 4:

price = 0

elif age < 18:

price = 5

elif age < 65:

price = 10

else:

price = 5

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

----------------------------------------------------------------

Your admission cost is $5.

检查特殊元素

判断列表中的元素并指定某个元素,进行判断

-------------------------------------------------------------------------------------------

requsested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requsested_topping in requsested_toppings:

if requsested_topping == 'green peppers':

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

else:

print("Adding " + requsested_topping + ".")

print("\nFinished making your pizza!")

-------------------------------------------------------------------------------------------

Adding mushrooms.

Sorry, we are out of green peppers right now.

Adding extra cheese.

Finished making your pizza!

在 循环之前先进行一个 if 判断

if requested_toppings 意识是对列表进行判断,列表中至少有一个元素时,返回 True,

现在这个列表为空,返回值为 False,打印 else 代码块中的内容

--------------------------------------------------------------

requested_toppings = []

if requested_toppings:

for requested_topping in requested_toppings:

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

print("\nFinished making your pizza!")

else:

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

----------------------------------------------------------------

Are you sure you want a plain pizza?

使用多个列表

对 requested_toppings 进行遍历,和 available_toppings 列表中的元素进行比较

-------------------------------------------------------------------------------------

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("\nFinished making your pizza!")

--------------------------------------------------------------------------------------

Adding mushrooms.

Sorry, we don't have french fries.

Adding extra cheese.

Finished making your pizza!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值