Files
2025PY/day04/01-作业.py
2025-05-22 16:50:44 +08:00

22 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 一.为抵抗洪水战士连续作战89小时编程计算共多少天零多少小时
hours = 89 # 定义变量hours存储时间
day = int(hours / 24) # int将括号里面的值转换成整数
h = hours % 24 # 求不足一天的小时数
# 打印结果:涉及到字符串和变量的拼接
# 利用+号
# 值必须是字符串才能使用+号进行拼接,使用str函数进行转换
print('为抵抗洪水战士连续作战89小时编程计算共'+ str(day) +'天零' + str(h) +'小时')
# 将上面的题目变得更加灵活
# 为抵抗洪水战士连续作战89小时编程计算共多少天零多少小时
# 将89变成由用户来输入
xiaoshi = int(input('请输入战士连续作战的小时数:')) # 注意这里输入的数字是字符串,利用转换函数进行字符串转数字
day = int(xiaoshi / 24) # 天,必须是整数
hour = xiaoshi % 24 # 取余获取不足一天的小时数
print('为抵抗洪水,战士连续作战'+str(xiaoshi)+'小时,编程计算共'+str(day)+'天零'+str(hour)+'小时')