39.7k views
1 vote
Set up a python program

Write a program to input the temperature every day for a certain number of weeks. The user will
input the number of weeks. You must use nested loops, otherwise there is no credit. Print the average
for each week when the input for the week is completed. Print the overall average at the end.

User Janneth
by
5.0k points

1 Answer

4 votes

Answer:

Here is a sample program that does what you have described:

# Get the number of weeks from the user

weeks = int(input("Enter the number of weeks: "))

# Initialize variables to store the total temperature and number of days

total_temp = 0

total_days = 0

# Loop through each week

for week in range(weeks):

# Get the number of days in the week from the user

days = int(input("Enter the number of days in week {}: ".format(week + 1)))

# Initialize a variable to store the weekly total temperature

weekly_total_temp = 0

# Loop through each day

for day in range(days):

# Get the temperature from the user

temperature = float(input("Enter the temperature for day {}: ".format(day + 1)))

# Add the temperature to the weekly total

weekly_total_temp += temperature

# Calculate the average temperature for the week

weekly_avg = weekly_total_temp / days

# Add the weekly total temperature to the overall total

total_temp += weekly_total_temp

# Add the number of days to the overall total

total_days += days

# Print the average temperature for the week

print("The average temperature for week {} is {:.2f}".format(week + 1, weekly_avg))

# Calculate the overall average temperature

overall_avg = total_temp / total_days

# Print the overall average temperature

print("The overall average temperature is {:.2f}".format(overall_avg))

Step-by-step explanation:

This program will ask the user to input the number of weeks, and then for each week, it will ask the user to input the number of days and the temperature for each day. It will then calculate and print the average temperature for each week, and finally calculate and print the overall average temperature.

I hope this helps! Let me know if you have any questions.

User Tonisha
by
4.5k points