《Python编程:从入门到实践》习题答案——第5章 if语句

《Python编程从入门到实践》习题答案及重点 发现自己对于python的基础掌握的并不是很牢实,利用几天时间把重点写下来,并打算把(《Python编程从入门到实践》试一试)的代码全部敲一遍,如果有不对的地方,请不吝赐教。 目录 第1章 起步 第2章 变量和简单数据类型 第3章 列表简介 第4章 操作列表 第5章 if语句 第6章 字典 第7章 用户输入和while循环 第8章 函数 第9章 类 第10章 文件和... 阅读详情
AI摘要

博客围绕Python开发语言展开,但具体内容缺失。Python是一种广泛应用的开发语言,在后端开发等众多领域发挥着重要作用。

# 5-1 条件测试:
car = 'subaru'
print(car == 'subaru')
print(car == 'audi')

num = 8
print(num > 10)
print(num < 5)

# 5-2 更多的条件测试:
# 1.检查两个字符串相等和不等
str1 = 'kan'
str2 = 'can'
print(str1 == str2)
print(str1 != str2)

# 2.使用函数lower()的测试
str1= "aaaaa"
str2 = "AAAAA"
print(str2.casefold())             
print(str2.lower())                
print(str1 == str2.casefold())
print(str1 == str2.lower())

# 3.检查两个数字相等、不等、大于、小于、大于等于和小于等于
num1 = 15
num2 = 33
print(num1 == num2)
print(num1 != num2)
print(num1 > num2)
print(num1 < num2)
print(num1 >= num2)
print(num1 <= num2)

# 4.使用关键字and和or的测试
num = 22
if (num > 20) and (num < 30):
    print(True)
else:
    print(False)

num = 66
if (num < 33) or (num > 55):
    print(True)
else:
    print(False)

# 5.测试特定的值是否包含在列表中
list = [1, 2, 3, 4, 5, 6]
if 1 in list:
    print(True)
else:
    print(False)

# 6.测试特点的值是否未包含在列表中
list = [1, 2, 3, 4, 5, 6]
if 10 not in list:
    print(True)
else:
    print(False)



# 5-3 外星人颜色:
print("----------版本1----------------")
alien_color = ['green', 'yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点\n")
print("----------版本2----------------")
alien_color = ['yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点")

# 5-4 外星人颜色2
print("----------版本1----------------")
alien_color = ['green', 'yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点")
else:
    print("获得10个点")

print("\n----------版本2----------------")
alien_color = ['yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点")
else:
    print("获得10个点")

# 5-5 外星人颜色3
print("----------版本1----------------")
alien_color = ['green', 'yellow', 'red']
if 'green' in alien_color:
    print("获得5个点")
elif 'yellow' in alien_color:
    print("获得10个点")
elif 'red' in alien_color:
    print("获得15个点")

print("\n----------版本2----------------")
alien_color = ['yellow', 'red']
if 'green' in alien_color:
    print("获得5个点")
elif 'yellow' in alien_color:
    print("获得10个点")
elif 'red' in alien_color:
    print("获得15个点")

print("\n----------版本3----------------")
alien_color = ['red']
if 'green' in alien_color:
    print("获得5个点")
elif 'yellow' in alien_color:
    print("获得10个点")
elif 'red' in alien_color:
    print("获得15个点")

# 5-6 人生的不同阶段:
age = int(input("请输入您的年龄:"))

if age < 2:
    print("他是个婴儿。")

elif 2 <= age < 4:
    print("他正在蹒跚学步。")

elif 4 <= age < 13:
    print("他是儿童。")

elif 13 <= age < 20:
    print("他是青少年。")

elif 20 <= age < 65:
    print("他是成年人。")

else :
    print("他是老年人。")

# 5-7 喜欢的水果:
favorite_fruits = ['apple', 'banana', 'orange']
if 'apple' in favorite_fruits:
    print(f"You really like {favorite_fruits[0]}")
if 'banana' in favorite_fruits:
    print(f"You really like {favorite_fruits[1]}")
if 'orange' in favorite_fruits:
    print(f"You really like {favorite_fruits[2]}")
if 'pear' in favorite_fruits:
    print(f"You really like pear")
if 'cherry' in favorite_fruits:
    print(f"You really like cherry")


# 5-8 以特殊方式跟管理员打招呼:
names = ['bob', 'admin', 'alice', 'jake', 'licy']
for name in names:
    if name == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello " + name.title() + ", thank you for logging in again.")

# 5-9 处理没有用户的情形:
names = ['bob', 'admin', 'alice', 'jake', 'licy']
if names:   
    for name in names:
        if name == 'admin':
            print("Hello admin, would you like to see a status report?")
        else:
            print("Hello " + name + ", thank you for logging in again.")
else:      
    print("We need to find some users!")
print(names)
names.remove('bob')
names.remove('admin')
names.remove('alice')
names.remove('jake')
names.remove('licy')    
print(names)

# 5-10 检查用户名:
current_users = ['bob', 'admin', 'alice', 'jake', 'licy']
new_users = ['anlen', 'Alice', 'admin', 'demo', 'nomi']
for new_user in new_users:
    if new_user.lower() in current_users:
        print(f"{new_user}该用户名已被使用,请换一个用户名。")
    else:
        print(f"{new_user}该用户名未被使用。")

# 5-11 序数:
lists = list(range(1, 10))
for list in lists:
    if list == 1:
        print('1st')
    elif list == 2:
        print('2nd')
    elif list == 3:
        print('3rd')
    else:
        print(f"{list}th")

# 5-12 设置if语句的格式

# 5-13 自己的想法

《Python编程:从入门到实践》各章习题答案汇总 阅读详情

相关推荐

Python编程:从入门到实践——练习题答案(第五章)

第5章 if语句 5-1 条件测试:编写一系列条件测试;将每个测试以及你对其结果的预测和实际结果都打印出来。你编写的代码应类似于下面这样: car = ‘subaru’ print(“Is car == ‘subaru’? I predict True.”) print(car == ‘subaru’) print("\nIs car == ‘audi’? I predict False.") p...

森豪Steven的博客 9949

《python语言程序设计基础》(第二版)第五章课后习题参考答案

《python语言程序设计基础》(第二版)第五章课后习题参考答案

xqhd的博客 1万+

Python编程:从入门到实践——练习题答案(第七章)

第7章 用户输入和while循环 7-1 汽车租赁:编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息, 如“Let me see if I can find you a Subaru”。 car = input('What kind of car do you want to rent?') print('Let me see if I can find you a ' + car) 运...

森豪Steven的博客 7298

python核心编程第五章习题

5-1 整形 讲讲 Python 普通整型和长整型的区别 python有三种整数类型:布尔型,标准整数类型,长整型 Python标准整数类型是最通用的数字类型。在大多数32位机器上,标准整数类型的取值范围:-2147483648到2147483647 Python标准整数类型等价于C的有符号长整型。八进制整数以数字“0”开始。十六进制整数以“0x”或“0X”开始 。 ...

aas96581的博客 413

《python编程从入门到实践》练习答案(2)

假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color的变量,并将其设置为'green'、'yellow'或'red'。能够通过测试的版本:alien_color = 'green'if alien_color == 'green': print("You just earned 5 points!")输出:You just earned 5 points!不能通过测试的版本:alien_color = 'red'if alien_color == 'green': print("

2401_89872449的博客 2173

Python编程从入门到实践第7章习题答案

#7-1汽车租赁 #coding:gbk #7-1汽车租赁 sorts = input("Please enter the kind of the car") print('Let me see if I can find you a ' + sorts) #7-2餐馆订位 number = input("Please enter the number of the catten") numbe...

qq_42744540的博客 794

python第五章课后编程题_《Python编程 从入门到实践》第五章课后习题

5-2str1 = "A"str2 = "a"print(str1 == str2)print(str1.lower() == str2)# change "char" to intnum1 = ord(str1)num2 = ord(str2)print(num1 == num2)print(num1 != num2)print(num1 > num2)print(num1 < nu...

weixin_42523718的博客 1469

《Python编程从入门到实践》课后题答案第5章if语句

** 练习5-3外星人的颜色#1 alien_color = "green" if alien_color == "green": print("You get 5 points!") else: print("You get 10 points!") 练习5-4外星人颜色#2 alien_color = "red" if alien_color == "green": ...

shg11111的博客 417

《Python编程:从入门到实践》第5章 if语句

《Python编程:从入门到实践》第5章 if语句 主要内容及主要习题

weixin_63664072的博客 2289

Python编程从入门到实践第10章习题答案

#10-1Python学习笔记 file_path = 'E:\python_work\example\learing_python.txt' with open(file_path) as file_object: #print(file_object.read()) lines = file_object.readlines() # for line in lines: # pr...

qq_42744540的博客 1310

Python编程从入门到实践第6章习题答案

#coding:gbk alien_0 = {'color':'green','point':5} get_point = alien_0['point'] print('You have get ' + str(get_point) + ' points') alien_0['x_position'] = 0 alien_0['y_position'] = 25 print('\n') pri...

qq_42744540的博客 785

Python编程从入门到实践 第五章:if语句 练习答案记录

Python编程从入门到实践 第五章:if语句 练习答案记录

Is_Superman的博客 2033

coursera Python交互编程入门 习题答案

以下代码是我的答案。仅供参考交流

King658的专栏 2540

Python编程从入门到实践 学习笔记+练习题答案导航

Python编程从入门到实践 练习题答案导航

Is_Superman的博客 990

Python编程从入门到实践 -----第5章、if(课后习题答案)

5-1 条件测试 : 编写一系列条件测试; 将每个测试以及你对其结果的预测和实际结果都打印出来。 详细研究实际结果, 直到你明白了它为何为True 或False 。 创建至少10个测试, 且其中结果分别为True 和False 的测试都至少有5个。 car = 'book' cars = ['book','admin'] #True print(car == 'book') #Fal...

weixin_44506064的博客 1587

Python编程:从入门到实践(配套源码及练习答案)

 本书是一本针对所有层次的Python读者而作的Python入门书。全书分两部分:第一部分介绍用Python编程所必须了解的基本概念,包括matplotlib、NumPy和Pygal等强大的Python库和工具介绍,以及列表、字典、if语句、类、文件与异常、代码测试等内容;第二部分将理论付诸实践,讲解如何开发三个项目,包括简单的Python 2D游戏开发,如何利用数据生成交互式的信息图,以及创建和定制简单的Web应用,并帮读者解决常见编程问题和困惑。 本书适合对Python感兴趣的任何层次的读者阅读。   PDF下载:http://www.okbase.net/file/item/347

python编程从入门到实践答案二

编写5条if语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color的变量,并将其设置为’green’、‘yellow’或’red’。·再创建一个包含5个用户名的列表,将其命名为new_users,并确保其中有一两个用户名也包含在列表current_users中。创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if语句,检查列表中是否包含特定的水果。

Jonathan的博客 2350

53253-Python机器学习编程与实战-习题数据和答案.rar.rar

53253-Python机器学习编程与实战-习题数据和答案.rar

上一篇: 《Python编程:从入门到实践》习题答案——第4章 操作列表
下一篇: 《Python编程:从入门到实践》习题答案——第6章 字典
广寒尔
博客等级 码龄3年 72粉丝 17原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值