ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python习题四--5.1~5.13

2022-02-02 22:06:22  阅读:264  来源: 互联网

标签:False get Python car alien -- points print 习题


5-1 条件测试 :编写一系列条件测试;将每个测试以及你对其结果的预测和实际结果都打印出来。你编写的代码应类似于下面这样
car = ‘subaru’
print(“Is car == ‘subaru’? I predict True.”)
print(car == ‘subaru’)
print("\nIs car == ‘audi’? I predict False.")
print(car == ‘audi’)
详细研究实际结果,直到你明白了它为何为True 或False 。
创建至少10个测试,且其中结果分别为True 和False 的测试都至少有5个。

car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
cars=['audi','bmw','subaru']
print("\nIs audi in cars? I predict True.")
print('audi'in cars)
print("\nIs pizza in cars? I predict False.")
print('pizza'in cars)
print("\nIs bmw in cars? I predict False.")
print('bmw'in cars)
#创建10个同理
Is car == 'subaru'? I predict True.
True

Is car == 'audi'? I predict False.
False

Is audi in cars? I predict True.
True

Is pizza in cars? I predict False.
False

Is bmw in cars? I predict False.
True

Process finished with exit code 0

5-2 更多的条件测试 :你并非只能创建10个测试。如果你想尝试做更多的比较,可再编写一些测试,并将它们加入到conditional_tests.py中。对于下面列出的各种测试,至少编写一个结果为True 和False 的测试。
··检查两个字符串相等和不等。
··使用函数lower() 的测试。
··检查两个数字相等、不等、大于、小于、大于等于和小于等于。
·· 使用关键字and 和or 的测试。
·· 测试特定的值是否包含在列表中。
··测试特定的值是否未包含在列表中。

#1检查两个字符串相等和不等。
car = 'Subaru'
print("Is car == 'subaru'? ")
print(car == 'subaru')
#2使用函数lower()测试。
print("\nIs car=='subaru'?")
print(car.lower() == 'subaru')

#3检查两个数字相等、不等、大于、小于、大于等于和小于等于。
age_0=18
age_1=20
print(age_0==age_1)
print(age_0!=age_1)
print(age_0>age_1)
print(age_0<age_1)
print(age_0>=age_1)
print(age_0<=age_1)
#4使用关键字and和or的测试。
print(age_0>=21 and age_1<=19)
print(age_1>=8 or age_0<=67)
#5试特定的值是否包含在列表中
cars=['ss','dd','ff']
print('ss' in cars)
#6试特定的值是都未包含在列表中。
if 'hh' not in cars:
    print('ss' not in cars)

D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
Is car == 'subaru'? 
False

Is car=='subaru'?
True
False
True
False
True
False
True
False
True
True
False

Process finished with exit code 0

5-3 外星人颜色 #1 :假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为’green’ 、‘yellow’ 或’red’ 。
··编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。
··编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。

alien_color='green'
if alien_color=='green':
	print('You get 5 points.')

alien_color='red'
if alien_color=='green':
	print('You get 5 points.')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
You get 5 points.

Process finished with exit code 0

5-4 外星人颜色 #2 :像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。
··如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。
··如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。
·· 编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。

alien_color='green'
if alien_color=='green':
	print('You get 5 points.')
else:
    print('You get 10 points.')


alien_color='red'
if alien_color=='green':
	print('You get 5 points.')
else:
    print('You get 10 points.')

5-5 外星人颜色 #3 :将练习5-4中的if-else 结构改为if-elif-else 结构。
··如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。
·· 如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。
·· 如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。
··编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。

alien_color='green'
if alien_color=='green':
	print('You get 5 points.')
elif alien_color=='yellow':
    print('You get 10 points.')
else:
    print('You get 15 points.')

alien_color='yellow'
if alien_color=='green':
	print('You get 5 points.')
elif alien_color=='yellow':
    print('You get 10 points.')
else:
    print('You get 15 points.')

alien_color='red'
if alien_color=='green':
	print('You get 5 points.')
elif alien_color=='yellow':
    print('You get 10 points.')
else:
    print('You get 15 points.')

D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
You get 5 points.
You get 10 points.
You get 15 points.

Process finished with exit code 0

5-6 人生的不同阶段 :设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。
··如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。
··如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
··如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。 ··如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。
··如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。
·· 如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。

age=1
if age<2:
	print('\nHe is a baby.')
elif age<4:
	print('\nHe is a toddler.')
elif age<13:
	print('\nHe is a child.')
elif age<20:
	print('\nHe is a teenager.')
elif age<65:
	print('\nHe is an adult.')
else:
	print('\nHe is a senior man.')
	

5-7 喜欢的水果:创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。 将该列表命名为favorite_fruits ,并在其中包含三种水果。
编写5条if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!”。

favorite_fruits=['apple','peach','banana']
if 'apple' in favorite_fruits:
    print('You really like apple!')
if 'peach' in favorite_fruits:
    print('You really like peach!')
if 'banana' in favorite_fruits:
    print('You really like banana!')

5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为’admin’ 。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。
··如果用户名为’admin’ ,就打印一条特殊的问候消息,如“Hello admin, would you liketo seeastatus report?”。
··否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。``

users=['xx','cc','vv','admin','ff']
for user in users:
    if user=='admin':
        print('Hello admin,would you like to see a status report?')
    else:
        print('Hello Eric, thank you for logging in again')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
Hello Eric, thank you for logging in again
Hello Eric, thank you for logging in again
Hello Eric, thank you for logging in again
Hello admin,would you like to see a status report?
Hello Eric, thank you for logging in again

Process finished with exit code 0

5-9 处理没有用户的情形 处 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
·· 如果为空,就打印消息“We need to find some users!”。
··删除列表中的所有用户名,确定将打印正确的消息。

users=[]
if users:
 for user in users:
    if user=='admin':
        print('Hello admin,would you like to see a status report?')
    else:
        print('Hello Eric, thank you for logging in again')
else:
    print('We need to find some users')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
We need to find some users

Process finished with exit code 0

5-10 检查用户名 :按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
·· 创建一个至少包含5个用户名的列表,并将其命名为current_users 。
··再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。
··遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。
··确保比较时不区分大消息;换句话说,如果用户名’John’ 已被使用,应拒绝用户名’JOHN’ 。

current_users=['Aa','ss','dd','ff','gg']
new_users=['aA','ss','zz','xx','cc']
for new_user in new_users:
    if new_user.lower() in [current_user.lower() for current_user in current_users]:
        print("please enter anothor name")
    else:
        print("the name is not used")
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
please enter anothor name
please enter anothor name
the name is not used
the name is not used
the name is not used

Process finished with exit code 0

5-11 序数:序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
··在一个列表中存储数字1~9。
··遍历这个列表。
··在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序 数都独占一行。

numbers=['1','2','3','4','5','6','7','8','9']
for i in numbers:
    if i=='1':
        print('1st')
    elif i=='2':
        print('2nd')
    elif i=='3':
        print('3rd')
    else:
        print(i+'st')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
1st
2nd
3rd
4st
5st
6st
7st
8st
9st

Process finished with exit code 0

在本章中,你学习了如何编写结果要么为Ture 要么为False 的条件测试。
你学习了如何编写简单的if 语句、if-else 语句和if-elif-else 结构。
在程序中,你使用了这些结构来测试特定的条件,以确定这些条件是否满足。
你学习了如何在利用高效的for 循环的同时,以不同于其他元素的方式对特定的列表元素进行处理。
你还再次学习了 Python就代码格式方面提出的建议,这可确保即便你编写的程序越来越复杂,其代码依然易于阅读和理解。
在第6章,你将学习Python字典。字典类似于列表,但让你能够将不同的信息关联起来。你将学习如何创建和遍历字典,以及如何将字典同列表和if 语句结合起来使用。学习字典让你能够模拟更多现实世界的情形。

标签:False,get,Python,car,alien,--,points,print,习题
来源: https://blog.csdn.net/fcxgfdjy/article/details/122762869

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有