Tyrone Biggums
Tyrone Biggums

Reputation: 1

How to store arbitrary number of inputs from user?

Just wondering how I would store the inputs for an average sum of square roots problem.

The user can input as many numbers as they until they type 'end'

here is what i have for that

while(True):
    x = input("Enter a number please:")
    if x == 'end':
        break

Upvotes: 0

Views: 123

Answers (3)

Sven Hakvoort
Sven Hakvoort

Reputation: 3621

If you mean that you want to store all the x values given, just use a list. l = [] l.append(x).

Upvotes: 0

Pythonista
Pythonista

Reputation: 11625

You can append these values to a list. Sum and then divide by the length for the average.

nums = []
while True:
    x = input("Enter a number:")
    if x == 'end':
        avg = sum(nums) / len(nums)
        print("And the average is....", avg)
        break
    elif x.isdigit():
        nums.append(int(x))
    else:
        print("Try again.")

Upvotes: 3

Kat
Kat

Reputation: 4695

The next thing you'd want to learn about is a data structure such a list. You can then add items to the list on the fly with list.append.

However, it's noteworthy that where ever you're learning Python from will certainly go over lists at one point or another.

Upvotes: 1

Related Questions