Python学习日志-4

今天分享的是第五章的要点和部分课后习题的参考代码。

要点:

1、逻辑判断符号和逻辑连词and和or.

2、检查特定值是否(不)包含在列表中用(not in).

3、if语句、if-else语句、if-elif-else结构、多个elif语句的使用、省略else代码块的用法.

4、if语句处理列表.

参考代码:

5-1 略

5-2 略

5-3 

#5-3
alien_color = 'green'
if alien_color == "green":
	print("You've got 5 points")

if alien_color == 'red':
	print("You've got 5 points")

	

运行结果:

You've got 5 points
[Finished in 0.0s]

5-4

#5-4
alien_color = 'green'
if alien_color == "green":
	print("You've got 5 points for killing the green one")
else:
	print("You've got 10 points")

if alien_color == 'red':
	print("You've got 5 points for killing the red one")
else:
	print("You've got 10 points")

运行结果:

You've got 5 points for killing the green one
You've got 10 points
[Finished in 0.0s]

5-5

#5-5
alien_color = 'green'
if alien_color == "green":
	print("You've got 5 points. And the alien is green.")
elif alien_color == "yellow":
	print("You've got 10 points.")
else:
	print("You've got 15 points.")

if alien_color == "yellow":
	print("You've got 5 points. And the alien is yellow.")
elif alien_color == "red":
	print("You've got 10 points.")
else:
	print("You've got 15 points.")

if alien_color == "red":
	print("You've got 5 points. And the alien is red.")
elif alien_color == "green":
	print("You've got 10 points.")
else:
	print("You've got 15 points.")

运行结果:

You've got 5 points. And the alien is green.
You've got 15 points.
You've got 10 points.
[Finished in 0.0s]

5-6

#5-6
age = 4
if age < 2:
	print("He is a baby.")
elif age >= 2 and age < 4:
	print("He is learning to walk.")
elif age >= 4 and age < 13:
	print("He is a kid.")
elif age >= 13 and age < 20:
	print("He is a teenager")
elif age >= 20 and age < 65:
	print("He is an adult.")
else:
	print("He is an old man.")

运行结果:

He is a kid.
[Finished in 0.0s]

5-7

favorite_fruits = ["apple" , "pear" , "banana"]
if "apple" in favorite_fruits:
	print("You really like apple.")
if "lemon" in favorite_fruits:
	print("You really like lemon.")
if "pear" in favorite_fruits:
	print("You really like pear.")
if "banana" in favorite_fruits:
	print("You really like banana.")
if "grape" in favorite_fruits:
	print("You really like grape.")

运行结果:

You really like apple.
You really like pear.
You really like banana.
[Finished in 0.0s]

5-8

users = ['admin',"Lucy","Jack","Lily","Tom"]
for user in users:
	if user == "admin":
		print("Hello admin, would you like to see a status report?")
	else:
		print("Hello " + user + ", thank you for logging in again.")

运行结果:

Hello admin, would you like to see a status report?
Hello Lucy, thank you for logging in again.
Hello Jack, thank you for logging in again.
Hello Lily, thank you for logging in again.
Hello Tom, thank you for logging in again.
[Finished in 0.0s]

5-9

users = ['admin',"Lucy","Jack","Lily","Tom"]
for user in users:
	if user == "admin":
		print("Hello admin, would you like to see a status report?")
	else:
		print("Hello " + user + ", thank you for logging in again.")

del users[:]

if not users:
	print("We need to find some users!")

运行结果:

Hello admin, would you like to see a status report?
Hello Lucy, thank you for logging in again.
Hello Jack, thank you for logging in again.
Hello Lily, thank you for logging in again.
Hello Tom, thank you for logging in again.
We need to find some users!
[Finished in 0.0s]

5-10

current_users = ['admin',"Lucy","Jack","Lily","Tom"]
new_users = ['admin',"Jackson","Jack","Oliver","Bob"]

for user in current_users:
	user = user.title()

for user in new_users:
	user = user.title()


for user in new_users:
	if user in current_users:
		print(user + ", You need to change the name.")
	else:
		print(user + ", your name is not used yet.")

运行结果:

admin, You need to change the name.
Jackson, your name is not used yet.
Jack, You need to change the name.
Oliver, your name is not used yet.
Bob, your name is not used yet.
[Finished in 0.0s]

5-11

numbers = range(1,11)

for number in numbers:
	if(number == 1):
		print(str(number) + "st")
	elif(number == 2):
		print(str(number) + "nd")
	elif(number == 3):
		print(str(number) + "rd")
	else:
		print(str(number) + "th")

运行结果:

1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
[Finished in 0.0s]


猜你喜欢

转载自blog.csdn.net/weixin_38224302/article/details/79644767