[Solved] How to write a program that prints the sum of ten numbers that the user has to enter


The way you wrote it, the script will end after the first error.

If you’d like to check for a string, display a message and still ask for input, you could try something like this:

i = 0
total = 0
while i < 10:
    try:
        a = int(input(f'Enter a number (position {i}): '))
        total += a
        i += 1
    except ValueError:
        print('You entered a string. Try again.')
print(f'\nThe sum is: {total}')

It will run until i reaches 10, but we only increment i when the input is valid.

Also, avoid using a variable named sum since it’s already a native function in Python.

Feel free to ask anything, hope it helps.

2

solved How to write a program that prints the sum of ten numbers that the user has to enter