-2

I am a beginner in python and i am here confused why sum function isn't working on adding array elements here is the code below `from future

inc = 0
array = []
i=0
while True:
rating=input("Enter your rate between (1 to 5) ")
inc += 1
array.insert(inc,rating)  
length = len(array)
print('total number of rating:',length)
ans = sum(array)
average = ans/length
print('avrage ratiing',round(average, 1))
while i<length:
     print(array)
     break`

please help

here is the traceback

  • 3
    What does "not working" mean? Do you get an error - please include the full traceback? It's hard to understand the code currently because the indentation is off - please fix that. – roganjosh Nov 02 '18 at 10:22
  • i just put the trace back please take a look now – Roasted_Ginger Nov 02 '18 at 10:28
  • 1
    Don't post images of errors/tracebacks. Copy and paste them – FHTMitchell Nov 02 '18 at 10:28
  • Enter your rate between (1 to 5) 3 total number of rating: 1 Traceback (most recent call last): File "insert.py", line 14, in ans = sum(a) TypeError: unsupported operand type(s) for +: 'int' and 'str' – Roasted_Ginger Nov 02 '18 at 10:30
  • 1
    [How can I read inputs as integers?](//stackoverflow.com/q/20449427) – Aran-Fey Nov 02 '18 at 10:36
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – FHTMitchell Nov 02 '18 at 10:37

2 Answers2

1

Add int casting

rating = int(input("Enter your rate between (1 to 5) "))

When you get an input from the user, you get it as type of str.

In order to increment this number, you can cast it to int and than you can apply math operations.

In Addition:

You can add a functionality that checks that the user actually inserted a number by:

def get_number():
  try:
   return int(input("Enter your rate between (1 to 5) "))
  except ValueError:
   raise ValueError("That's not an integer, please insert an integer")

And than:

rating = get_number()

EDIT:

Notice that if you want to get input like '3.7' you should use float casting.

return float(input("Enter your rate between (1 to 5) "))
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
  • Your `get_number` function returns `None` on failure which will just cause an error later on. It's better to either retry or `except ValueError: print(...); return get_number()` or reraise the error `raise`? It'll also tell me that `3.14` is "not a number". – FHTMitchell Nov 02 '18 at 10:31
  • Sure, Added it. Thanks – omri_saadon Nov 02 '18 at 10:34
  • @FHTMitchell, That depends whether he wants an integer input or a float input. Anyway, i've showed him how to get an input like 3.7 by using the `float` casting. Also changes the text to integer instead of number in case of `int` casting. – omri_saadon Nov 02 '18 at 10:37
  • i also have another small programe can you check this oyt – Roasted_Ginger Nov 02 '18 at 10:41
  • here the error is "relab = float(1/2(fairness + (1-(score) - goodness)/2)) TypeError: 'int' object is not callable" – Roasted_Ginger Nov 02 '18 at 10:43
  • @Roasted_Ginger , You forgot the operand between `1/2` and the rest of your expression. `float(1/2 ?Insert operand in here? (fairness + (1-(score) - goodness)/2)) ` – omri_saadon Nov 02 '18 at 10:45
  • Thanks i am really noob -_- – Roasted_Ginger Nov 02 '18 at 11:00
1

The sum function is, effectively, defined like so

def sum(iterable, start=0):
    total = start
    for item in iterable:
        total += item
    return total

Currently, your array is a list of strings, because that is what is returned by input, so the sum function tries to add a string item to an integer total giving the error

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

The trick is to cast the string to an int

rating = int(input("Enter your rate between (1 to 5) "))
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47