1

Python noob here. I am trying to create a list with numbers a user inputs and then do some simple calculations with the numbers in the list at the end, in a while loop. The While loop is not breaking when 'done' is inputted. It just prints 'Invalid input.'

list = []
while True:
    try:
        n = int(input('Enter a number: '))
        list.append(n)
    except:
        print('Invalid input') 
    if n == 'done':
        break

print(sum.list())
print(len.list())
print(mean.list())
1
  • 1
    The only way any value gets assigned to n is after applying int() to the user's input. There is no conceivable return value from int() that is equal to 'done'... Commented Apr 12, 2019 at 1:41

4 Answers 4

1

You will have to separate receiving user input with checking against "done" from conversion to a number and appending to the list. And you will have to check for "done" before converting the input to an integer.

Try something like this:

list_of_numbers = []

while True:
    user_input = input("Enter a number or 'done' to end: ")

    if user_input == "done":
        break

    try:
        number = int(user_input)

    except ValueError:
        print("invalid number")
        continue

    list_of_numbers.append(number)

print(list_of_numbers)

# further processing of the list here
Sign up to request clarification or add additional context in comments.

3 Comments

I like this approach however, I am getting an AttributeError: 'builtin_function_or_method' object has no attribute 'list'.
@antman well, this likely comes from some further processing on your part. I suspect you're doing something.list somewhere and something doesn't have a list attribute.
Yes, I was trying to print(sum.list()) instead of print(sum(list))....noob working late...Thanks!
1

This is because the int() function is trying to convert your input into an integer, but it is raising an error because the string 'done' can not be converted to an integer. Another point is that sum(), mean() and len() are functions, not attributes of lists. Also mean() is not a built in function in python, it must be import with numpy. Try it like this:

from numpy import mean
list = []
while True:
    try:
        n = input('Enter a number: ')
        list.append(int(n))
    except:
        if n!='done':
            print('Invalid input') 
    if n == 'done':
        break

print(sum(list))
print(len(list))
print(mean(list))

Comments

1

You must check if you can turn the input into a integer before appending to your list. You can use use the try/except to catch if the input variable is convertible to a integer. If it's not then you can check for done and exit.

list = []
while True:
    n = input('Enter a number: ')
    try:
        n = int(n)
        list.append(n)
    except ValueError:
        if n == 'done':
            break
        print('Invalid input') 

total = sum(list)
length = len(list)
mean = total/length

print('sum:', total)
print('length:', length)
print('mean:', mean)

Example interaction

Enter a number: 12
Enter a number: 3
Enter a number: 4
Enter a number:
Invalid input
Enter a number: 5
Enter a number:
Invalid input
Enter a number: done
sum: 24
length: 4
mean: 6.0

Comments

0

If the user enters done, you will attempt to convert into an int, which will raise an exception that you then catch.

Instead, perform your check before attempting to convert it to an integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.