Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Collecting multiple unknown inputs

Alright so this problem has been grinding me for a good hour. I am taking a zybooks course and I’m presented with the prompt,

Statistics are often calculated with varying amounts of input data. Write a program that takes any number of integers as input, and outputs the average and max.

Ex: If the input is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

15 20 0 5
the output is:

10 20
currently I have it ‘working’ with my code but the issue is I cannot figure out how to keep the input open for more or less inputs as zybooks runs through multiple different tests. i’m sure its something simple im overlooking. anything helps!

nums = []
for i in range(0, 4):
    number = int(input('Enter number'))
    nums.append(number)
avg = sum(nums) / len(nums)
print(max(nums), avg)

>Solution :

This code continually asks the user to enter values until they enter a blank (just press enter without typing).

This is the code:

nums = []

# initialse
number = 0
# loop until there isn't an input
while number != "":
    # ask for user input
    number = input('Enter number:')
    # validate the input isn't blank
    # prevents errors
    if number != "":
        # make input integer and add it to list
        nums.append(int(number))

avg = sum(nums) / len(nums)
print(max(nums), avg)

Alternatively, if you have the list of numbers:

def maxAndAvg(nums):
    avg = sum(nums) / len(nums)
    return max(nums), avg
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading