51、以下代码用于显示溶液酸度的消息:ph = float(input(“Enter the ph level: “)) if ph < 7.0: print(“It’s acidic!”) elif ph < 4.0: print(“It’s a strong acid!”) 当用户输入 6.4 时,会显示什么消息?
It’s acidic!
52、以下代码用于显示溶液酸度的信息:ph = float(input(“Enter the ph level: “)) if ph < 7.0: print(“It’s acidic!”) elif ph < 4.0: print(“It’s a strong acid!”) 当用户输入3.6时,会显示什么信息?
It’s acidic!
53、以下代码会显示溶液酸度的相关消息: ph = float(input("Enter the ph level: ")) if ph < 7.0: print("It's acidic!") elif ph < 4.0: print("It's a strong acid!") 对代码的某一行进行小改动,使得输入小于 4 的值时,两条消息都会显示。
将 elif ph < 4.0: 改为 if ph < 4.0:
54、导入数学模块(math),并编写一个表达式来计算 -2.8 的向下取整值。
使用 math 模块的 floor 函数,表达式为 math.floor(-2.8)
55、导入math模块,并编写一个表达式,对 -4.3 进行四舍五入,然后求出该结果的绝对值。
可以使用以下代码实现:import math; abs(round(-4.3))
56、导入math模块,并编写一个表达式来计算34.5的正弦值的上限(向上取整)。
可使用表达式 math.ceil(math.sin(34.5)) 来实现,前提是已导入 math 模块,即:
import math
57、导入日历模块。
要导入日历模块,可以使用以下 Python 代码:import calendar
58、使用 help 函数,读取日历模块中 isleap 函数的描述。
在 Python 中可以使用如下代码实现:
import calendar
help(calendar.isleap)
59、使用 dir 函数获取 calendar 模块包含的内容列表。
在 Python 代码中可以使用以下语句来实现:
import calendar
print(dir(calendar))
60、在日历模块中找到并使用一个函数,来确定2000年到2050年(含)之间有多少个闰年。
可使用 Python 日历模块的相关函数来解决。可通过循环遍历 2000 年到 2050 年的每一年,使用 isleap 函数判断是否为闰年并计数。示例代码如下:
import calendar
count = 0
for year in range(2000, 2051):
if calendar.isleap(year):
count = count + 1
print(count)
61、创建一个名为exercise.py的文件并写入给定代码。运行exercise.py,导入doctest模块并运行doctest.testmod()。
首先,创建一个名为 exercise.py 的文件,将以下代码写入该文件:
def average(num1: float, num2: float) -> float:
'''Return the average of num1 and num2.
>>> average(10,20)
15.0
>>> average(2.5, 3.0)
2.75
'''
return (num1 + num2) / 2
需要注意原答案中 return num1 + num2 / 2 计算平均的逻辑有误,正确的应该是:
return (num1 + num2) / 2
然后,运行 exercise.py 文件,在 Python 环境中导入 doctest 模块并运行 doctest.testmod() </

最低0.47元/天 解锁文章
167

被折叠的 条评论
为什么被折叠?



