Question: Here is another example computing the sum of a list of integers. Note that the code is somewhat different than the code computing the max

Here is another example computing the sum of a list of integers. Note that the code is somewhat different than the code computing the max even value. For computing the sum, the program initializes a variable sum to 0, then simply adds the current iteration's list element value to that sum.

Run the program below and observe the output. Next, modify the program to calculate the following:

Compute the average, as well as the sum. Hint: You don't actually have to change the loop, but rather change the printed value.

Print each number that is greater than 21.

Print each number that is greater than a number the user enters on a second line.

# User inputs string w/ numbers: '203 12 5 800 -10' user_input = input('Enter numbers:').strip()

tokens = user_input.split() # Split into separate strings

# Convert strings to integers print() nums = [] for pos, token in enumerate(tokens): nums.append(int(token)) print('%d: %s' % (pos, token))

sum = 0 for num in nums: sum += num

print('Sum:', sum)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!