Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

Understand why succession management is important.

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago

Question

What is job enlargement ?

Answered: 1 week ago