-2

I am learning python3 and I've hit a blocker regarding an exercise I am doing.

Here is the problem I am trying to solve:

"Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number."

Here is my answer:

numslist = list()

while True:
    x = input("Enter a number: ")
    numslist.append(x)
    if x is str:
        print ("bad data")
    if x == 'done':
        break

numslistt = numslist[0:-1]

count = 0
for a in numslistt:
    count = count + 1

total = 0
for thing in numslistt:
    total = total + thing

print(numslistt)
print(count)
print(total)

I get an error however when trying to add the numbers in the list? Can someone advise on what I am doing wrong?

Here is the Traceback I get:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    total = total + thing
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Thanks,

Samuel

Samuel Adjei
  • 9
  • 1
  • 5
  • 1
    Can you describe in more detail what happens when the program runs? – mkrieger1 Mar 02 '18 at 10:57
  • 1
    "I get an error however": it's always good include the error (and its traceback) in your question as well, next to the code causing it. –  Mar 02 '18 at 10:58
  • `is` tests object identity. It doesn't check if a variable has a special type. BTW, `x` will always be a string. – Matthias Mar 02 '18 at 10:59
  • Concerning calculating the sum and number of items in a list: Check out the [`sum`](https://docs.python.org/3/library/functions.html#sum) and [`len`](https://docs.python.org/3/library/functions.html#len) functions. – mkrieger1 Mar 02 '18 at 10:59
  • "if x is str": change that to `if isinstance(x, str):`. `x is str` is always `False` here. In fact, it's an unnecessary check, since the output from `input()` always returns a string. –  Mar 02 '18 at 11:00
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – mkrieger1 Mar 02 '18 at 11:04
  • 1
    I've added the traceback for those that have requested it - should of included it in the first post - sorry! – Samuel Adjei Mar 02 '18 at 11:33
  • Even if you don't use built-in functions for counting the number of list elements and thier sum, you don't need two for-loops for that. You could do both in one. – Psytho Mar 02 '18 at 11:40

1 Answers1

2

When you call input() you get back a string. You must convert your values to a number using int() or float() before you can sum them.

The test x is str doesn't do what you think: you are testing for x to be the actual str type, which it isn't. To test whether the value is an instance of str use isinstance(x, str).

BTW, you don't need a loop to count the numbers: there's an attribute on the list that will tell you that directly. Also you don't need a loop to sum them, there's a builtin function for that.

Duncan
  • 92,073
  • 11
  • 122
  • 156