What is the function for Varied amount input data for Python?

Question:

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:

15 20 0 5
the output is:

10 20

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)

All is gives me is Enter number:

Asked By: Jaeson Keller

||

Answers:

nums = []

# loop until there isn't an input
while not nums:
    # ask for user input
    number = input()
    nums = [int(x) for x in number.split() if x]

avg = int(sum(nums) / len(nums))
print(avg, max(nums))
Answered By: SM1312

I solved the problem correctly using this:

user_input = input()

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

nums = []                    
for token in tokens:         # Convert strings to integers
    nums.append(int(token))

avg = sum(nums) / len(nums)  # Calculates average of all integers in nums
print(int(avg), max(nums))   # Prints avg as an Integer instead of a Float
Answered By: Clara Blount
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.