Home > Back-end >  How do i create a number of inputs from an input
How do i create a number of inputs from an input

Time:11-21

I'm brand new to this, 10 days in. Ive been thinking how I could solve this for 30 min. Please help.

Find Average

You need to calculate the average of a collection of values. Every value will be valid number. The average must be printed with two digits after the decimal point.

Input-

On the first line, you will receive N - the number of the values you must read On the next N lines you will receive numbers.

Output-

On the only line of output, print the average with two digits after the decimal point.

Input
4
1
1
1
1
Output
1.00

Input 
3
2.5
1.25
3
Output
2.25

From what I see, I figure I need to create as much inputs as the N of the first one is and then input the numbers Id like to avarage and then create a formula to avarage them. I may be completely wrong in my logic, in any case Id be happy for some advice.

So far I tried creating a while loop to create inputs from the first input. But have no clue how to properly sintax it and continue with making the new inputs into variables I can use

a=int(input())
x=1
while x<a or x==a:
    float(input())
    x=x 1

CodePudding user response:

a=int(input('Total number of input: '))

total = 0.0

for i in range(a):
    total  = float(input(f'Input #{i 1}: '))
    
print('average: ', total/a)

Modified a bit on your version to make it work

CodePudding user response:

There were few things that you were doing wrong. when the numbers are decimals use float not int.

If you are looking for a single line input this should be how it's done.

When writing the code please use proper variables and add a string that's asking for the input.

total = 0

first_num=int(input("Number of inputs: "))
number = input("Enter numbers: ")
input_nums = number.split()
    

for i in range(first_num):
    total = total   int(input_nums[i])


average = total/first_num
print(average)

If you are looking for a multiline output This should be how it's done.


first_num=int(input("Number of inputs: "))
x=1
total = 0
while x<first_num or x==first_num:
    number = float(input("Enter numbers: "))
    total = total   number
    x=x 1

avg = total/first_num
print(avg)
  • Related