Files
004_comission/saralai19981321/task1/Q3/main.py
louiscklaw 04a4edce00 update,
2025-01-31 21:40:42 +08:00

59 lines
1.8 KiB
Python

#!/usr/bin/env python
import os,sys
from pprint import pprint
from math import sin, pi
import random
hours = 24
grid_num = 9
base_temperature=20
temperature_sections = []
def simulateTemperatureChange(hr_of_day, change_cofficient =1):
# 3. Use if-else statements to apply the temperature changes based on the time of day.
# morning (6-11)
# afternoon (12-17)
# evening (18-22)
# night (23-5)
if (hr_of_day >= 6 and hr_of_day <= 11):
change_cofficient = -1
elif (hr_of_day > 11 and hr_of_day <= 17):
change_cofficient = 3
elif (hr_of_day > 17 and hr_of_day <= 22):
change_cofficient = 2
else:
change_cofficient = 1
return base_temperature + (random.uniform(-change_cofficient ,change_cofficient) * sin(hr_of_day/24.0* pi))
def initRoomTemperature():
# 1. Initialize the temperature of the room.
return [[0]*grid_num]*hours
# 2. Use nested loops to update the temperature of each section of the room at each hour.
def updateTemperature():
# 5. Use a while loop to simulate the temperature changes over a 24-hour period.
hr_of_day = 0
while hr_of_day < hours:
for section in range(grid_num):
temperature_sections[hr_of_day][section] = simulateTemperatureChange(hr_of_day)
hr_of_day += 1
# 4. Print the temperature distribution in the room at each hour.
def printTemperatureOfRooms():
for hr_of_day in range(hours):
print(f'hour {hr_of_day+1}')
for section in range(0,grid_num,3):
print(f'{temperature_sections[hr_of_day][section]:.1f}', end=' ')
print(f'{temperature_sections[hr_of_day][section+1]:.1f}', end=' ')
print(f'{temperature_sections[hr_of_day][section+2]:.1f}', end=' ')
print()
print('')
temperature_sections=initRoomTemperature()
updateTemperature()
printTemperatureOfRooms()