Home > Back-end >  How can we add inputs when total number of inputs are not known using strings in Python?
How can we add inputs when total number of inputs are not known using strings in Python?

Time:05-09

I could get the inputs but don't know how to add them. This is what I tried:

n = int(input("enter the total number to find average"))

for i in range (0, n):
    i = int(input("enter the number {i} here"))

CodePudding user response:

if you want to be able to have an arbitrary number of inputs you can use a while loop and a break condition

numbers = []
i = 1
while True:
    try:
        numbers.append(int(input(f"enter number {i} (enter blank to end):")), inplace=True)
    except ValueError:
        break

in this way you can store as many or few inputs as the user wishes to provide in a list and then perform the rest of the script on that list

CodePudding user response:

Please try this:

n = int(input("enter the total number to find average"))
s=0     #a varaiable to store sum
for i in range (1, n 1):
    i = int(input("enter the number {} here ".format(i)))
    s =i
print('The sum of all the numbers entered by you is :', s)
  • Related