Problem error taking additional values from the Python list and averaging them

Asked 2 years ago, Updated 2 years ago, 21 views

num= [30, 29, 47, 10, 78]
a = input ("What number do you want to add?") #Accept a number to add
num.append(a) # Add accepted numbers using append among methods
plus = sum(num)
average = plus / len(num)
print(average)

If I input it, the number I received is added to the list, but after that, the error appears like this. Where and how should I fix it?

TypeError Traceback (most recent call last)
<ipython-input-17-b5963e133a37> in <module>()
      2 a = input ("Number to add?") #Accept a number to add
      3 num.append(a) # Add accepted numbers using append among methods
----> 4 plus = sum(num)
      5 average = plus / len(num)
      6 print(average)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

python

2022-09-20 10:51

1 Answers

input returns str object.

Please convert to int object.

b = []
a = input()
print(type(a))
>> <type 'str'>
b.append(int(a))


2022-09-20 10:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.