Jaeson Keller
Jaeson Keller

Reputation: 23

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

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:

Upvotes: 1

Views: 25414

Answers (7)

pinkflame
pinkflame

Reputation: 1

inp = input()
my_list = [int (i) for i in inp.split ()]
avg = sum(my_list) // len(my_list)
print(avg, max(my_list))

#use list comprehension to convert user input into a list then solve for the average and max

Upvotes: 0

LOGAN SCHINZING
LOGAN SCHINZING

Reputation: 1

This can be solved with a smaller amount of code using a list comprehension:

numbers = [int(i) for i in input().split()]
themax = max(numbers)
theave = sum(numbers) // len(numbers)

print(theave , themax)

Upvotes: 0

Caleb
Caleb

Reputation: 1

I came up with a simpler method with only three lines of code.

nums =[int(i) for i in ints.split()]

The first is this one with ins as my input. What is does is it will divide and instances or inputs what are integers from a split input. we don't however change our input though

average = int(sum(nums)/len(nums))

The second is this because it now will ask for the sum of our list divided by the length of the list which is the definition of average in this case.

Then the third one is you find the max using the max function but on your list which we made from the first variable list.

Upvotes: 0

Crazy_Candyman
Crazy_Candyman

Reputation: 1

Lab 6.12 ZyBooks. The instructions are very unclear and the system wants you to write a program that takes a input in the following manner (ex:20 1 4 6 18). Take that input and calculate an average and find max by converting input to a list. The following code worked successfully.

user_input = input().split() # input ex: 20 10 5 2 (turns into a list)
my_list = []

for num in user_input: # loops through inputs
    my_list.append(int(num)) # adds the inputs to list and converts to int.

avg = sum(my_list) // len(my_list)
list_max = max(my_list)

print(avg, list_max)

Upvotes: 0

smashie420
smashie420

Reputation: 1

Zybooks
9.16 LAB: Varied amount of input data

user_input = input().split(" ")
average = 0.00

for x in range(len(user_input)):
    average += float(user_input[x])
    user_input[x] = float(user_input[x])
average = float(average) / len(user_input)


print(f'{max(user_input):.2f} {round(average,2):.2f}')

Upvotes: 0

Clara Blount
Clara Blount

Reputation: 11

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

Upvotes: 1

SM1312
SM1312

Reputation: 588

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))

Upvotes: 0

Related Questions