-2

The error I get

Exception has occurred: TypeError
unsupported operand type(s) for +: 'int' and 'str'
  File "/home/QuartsPy/Desktop/Python Random/Missing.py", line 5, in main
    finished_data = sum(data)
  File "/home/QuartsPy/Desktop/Python Random/Missing.py", line 10, in <module>
    main()

I keep getting this^^ error I am trying to make a missing number finder. So I would enter in input 1,3,4 and it should send 2 in the console because that is the missing number. This is my code:

def main():
    data = input()
    n = len(data)
    total = (n + 1)*(n + 2)/2
    finished_data = sum(data)
    print(total - finished_data)


if __name__ == "__main__":
    main()
QuartsPy
  • 31
  • 6
  • Please add the error you are getting *as text* to the question. – chepner Jan 26 '21 at 17:55
  • That said, `data` is a `str` value. As an iterable, it's just a sequence of single-character strings; you need to convert *something* to, say, `int` values that can be added together. – chepner Jan 26 '21 at 17:56
  • `data` is just a string, you probably want to split it into individual numbers. Try `data = [int(x) for x in input().split()]` – tobias_k Jan 26 '21 at 17:57
  • I tried that but got this error: `Exception has occurred: ValueError invalid literal for int() with base 10: '1,2,4' File "/home/QuartzWarrior/Desktop/Python Random/Missing.py", line 2, in main data = int(input()) File "/home/QuartzWarrior/Desktop/Python Random/Missing.py", line 10, in main()` – QuartsPy Jan 26 '21 at 18:46
  • And this is why including all of the necessary information to reproduce your problem is needed. You'll want something like `data = [int(number) for number in input().split(',')]` – C.Nivs Jan 26 '21 at 18:48
  • @tobias_k I tried that but got this answer: `Exception has occurred: ValueError invalid literal for int() with base 10: '1,3,4' File "/home/QuartzWarrior/Desktop/Python Random/Missing.py", line 2, in data = [int(x) for x in input().split()] File "/home/QuartzWarrior/Desktop/Python Random/Missing.py", line 2, in main data = [int(x) for x in input().split()] File "/home/QuartzWarrior/Desktop/Python Random/Missing.py", line 10, in main()` – QuartsPy Jan 26 '21 at 18:48
  • Ok thanks, @C.Nivs this worked! – QuartsPy Jan 26 '21 at 18:50

3 Answers3

-1

You need to cast every element of your input data as int

def main():
        data = input()
        n = len(data)
        total = (n + 1)*(n + 2)/2
        finished_data = sum(int(n) for n in data)
        print(int(total - finished_data))
Ajeetkumar
  • 1,271
  • 7
  • 16
  • 34
  • With this, I get this error: `Exception has occurred: ValueError invalid literal for int() with base 10: ',' File "/home/QuartzWarrior/Desktop/Python Random/Missing.py", line 5, in finished_data = sum(int(n) for n in data) File "/home/ilmart06/Desktop/Python Random/Missing.py", line 5, in main finished_data = sum(int(n) for n in data) File "/home/QuartzWarrior/Desktop/Python Random/Missing.py", line 10, in main()` – QuartsPy Jan 26 '21 at 18:40
  • You didn't mention you are using delimiter comma. anyways people have posted the updated answers – Ajeetkumar Jan 27 '21 at 06:01
-1

You need to change the data type int to string.

def main():
data = input()
    n = len(data)
    # here you needs to convert datatype str to int
    total = int((n + 1)*(n + 2)/2)
    finished_data = sum(int(n) for n in data)
    print(total - finished_data)
Rahul kuchhadia
  • 289
  • 4
  • 10
-1

In Python 3, the result of input() is just a string, it is not evaluated as an integer, list or tuple.

Do do so, you have to str.split the list be the delimiter between the numbers, and then convert the parts to int, e.g. in a list comprehension. If the numbers are just separated by spaces, you can just use input().split(); it will split by all whitespaces by default.

In your case, judging by the error in your comments, they are separated by commas, so you have to explicitly provide the separator to split, i.e.:

data = [int(x) for x in input().split(',')]

You might also want to convert the result to int before printing, or use // instead of / so it does not become a float.

tobias_k
  • 81,265
  • 12
  • 120
  • 179