In: Computer Science
Ask the user how many days that they collected gems. A loop you write should loop how many days the user enters. If the user enters 5 days, then the loop should loop 5 times to collect the data from each day.
So, each loop iteration represents a day. In each loop iteration, ask the user how many gems they collected that day. After the loop finishes gathering the data for each day, calculate the total and average gems collected. Display the average as a float value.
TIP: To find the average, divide the sum of the numbers by the number of numbers. In the output below, the average is 15/5 = 3.0. Display the average as a float value.
Sample input/output (your output should match exactly):
How many days did you collect gems? 5
Enter the number of gems collected on day 1: 5
Enter the number of gems collected on day 2: 4
Enter the number of gems collected on day 3: 3
Enter the number of gems collected on day 4: 2
Enter the number of gems collected on day 5: 1
Total gems collected: 15
Average gems collected per day: 3.0
Another sample input/output (your output should match exactly):
How many days did you collect gems? 6
Enter the number of gems collected on day 1: 2
Enter the number of gems collected on day 2: 2
Enter the number of gems collected on day 3: 2
Enter the number of gems collected on day 4: 6
Enter the number of gems collected on day 5: 3
Enter the number of gems collected on day 6: 4
Total gems collected: 19
Average gems collected per day: 3.16
Source Code:
Output:
Code to Copy (refer above images of code for indentation):
#variables
total=0
average=0
#read how many days from user
gems=int(input("How many days did you collect gems? "))
for i in range(gems):
#read data from user
print("Enter the number of gems collected on day ",i+1,end=":
")
n=int(input())
#calculate total
total+=n
#calculate average
average=total/gems
#print total and average
print("Total gems collected: ",total)
print("Average gems collected per day: ",average)