用python语言编写程序输出一周的日最高气温。气温从键盘输入到数组。气温为华氏温度,范围在20-84之间
时间: 2024-02-15 19:04:25 浏览: 33
好的,以下是一个简单的 Python 代码示例,用于输出一周的日最高气温:
```python
temperatures = []
for i in range(7):
while True:
temp = int(input("请输入周{}的最高气温(华氏度):".format(i+1)))
if 20 <= temp <= 84:
temperatures.append(temp)
break
else:
print("输入的气温不在20到84之间,请重新输入!")
print("一周的日最高气温分别为:", temperatures)
```
这个程序会要求你分别输入一周七天的最高气温,如果输入的气温不在20到84之间,就会提示你重新输入。如果输入正确,程序会将气温添加到一个列表中,并在最后输出这个列表,即一周的日最高气温。
相关问题
编写程序输出一周的日最高气温。气温从键盘输入到数组。气温为华氏温度,范围在20-84之间。
好的,以下是一个简单的Python程序,可以实现您的需求:
```python
temps = []
for i in range(7):
temp = int(input("请输入星期{}的最高气温(范围在20-84之间): ".format(i+1)))
while temp < 20 or temp > 84:
temp = int(input("输入的气温不在范围内,请重新输入: "))
temps.append(temp)
max_temp = max(temps)
print("本周的最高气温为: {}华氏度".format(max_temp))
```
这个程序会依次提示用户输入7天的最高气温,如果输入的气温不在20-84之间,则会提示用户重新输入。输入完成后,程序会计算出这7天中的最高气温,并输出结果。
【问题描述】编写程序输出一周的日最高气温。气温从键盘输入到数组。气温为华氏温度,范围在20-84之间。
好的,我可以帮你编写这样的程序。你可以参考下面的代码实现:
```python
temperatures = [] # 定义一个空列表用于存储气温
# 输入每天的气温,共7天
for i in range(7):
temperature = float(input("请输入第%d天的气温(华氏度):" % (i+1)))
while temperature < 20 or temperature > 84: # 判断输入是否合法
temperature = float(input("输入有误,请重新输入第%d天的气温(20-84华氏度):" % (i+1)))
temperatures.append(temperature) # 将输入的气温添加到列表中
# 输出一周的日最高气温
max_temperature = max(temperatures) # 获取气温列表中的最大值
print("一周的日最高气温为:%.1f华氏度" % max_temperature)
```
这个程序会让你输入每天的气温,共7天,然后会判断输入是否在20-84之间,如果不在范围内会提示重新输入。最后,程序会输出一周的日最高气温,即气温列表中的最大值。
希望这个程序可以帮到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)