SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Repetition Structures
Exercise:
Programming Exercises
Question:1 | ISBN:9780132576376 | Edition: 2

Question

Bug Collector
A bug collector collects bugs every day for seven days. Write a program that keeps a running total of the number of bugs collected during the seven days. The loop should ask for the number of bugs collected for each day, and when the loop is finished, the program should display the total number of bugs collected.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

COMPLETE PROGRAM CODE:

# initialize a variable to store the total number of bugs
total_bugs = 0

# create a for loop that iterates 7 times
for day in range(7):
    # prompt the user to enter the number of bugs collected for each day
    bugs = int(input("Enter the number of bugs collected for day " + str(day + 1) + ": "))
    # add the number of bugs collected to the total
    total_bugs += bugs

# display the total number of bugs collected
print("The total number of bugs collected is", total_bugs)

OUTPUT 1:

Enter the number of bugs collected for day 1: 33
Enter the number of bugs collected for day 2: 2
Enter the number of bugs collected for day 3: 4
Enter the number of bugs collected for day 4: 6
Enter the number of bugs collected for day 5: 55
Enter the number of bugs collected for day 6: 2
Enter the number of bugs collected for day 7: 69
The total number of bugs collected is 171

OUTPUT 2:

Enter the number of bugs collected for day 1: 10
Enter the number of bugs collected for day 2: 20
Enter the number of bugs collected for day 3: 30
Enter the number of bugs collected for day 4: 50
Enter the number of bugs collected for day 5: 35
Enter the number of bugs collected for day 6: 25
Enter the number of bugs collected for day 7: 10
The total number of bugs collected is 180

 

 

0 0

Discussions

Post the discussion to improve the above solution.