Here is the code:

s = str(input()) a = int(s[:1])+int(s[2:]) 

The problem is this: the input will be [digit] + space + [digit] , and it seems that as soon as the input sees the digit it perceives everything as a number, and because of this an error occurs.

How do I specify the type of data that will be entered a specific moment?

This is what error shows:

SyntaxError: unexpected EOF while parsing

  • it is generally more correct to split a line with the split method: "1 3" .split ('') # ['1', '3'] - Denis

2 answers 2

input() in Python 2 is equivalent to eval(raw_input()) . The sequence digit, space, digit is not a valid Python code, which leads to SyntaxError :

 >>> eval("1 2") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 1 2 ^ SyntaxError: unexpected EOF while parsing 

Either use raw_input() or use Python 3, where input() behaves like raw_input() . In both cases, the str() call should be removed:

 #!/usr/bin/env python3 s = input('Введите два целых числа, разделённых пробелом: ') a, b = map(int, s.split()) # ... 

    Use raw_input :

     >>> def f(prompt): ... return sum(int(i) for i in raw_input(prompt).split()) ... >>> f('-->') -->1 2 33 555 591 >>> 

    It's not about the data types, and input and raw_input always give a string, just input reads up to the first space.

    • If I enter " for input() , it will also give an error, so it's not just in the spaces - andreymal
    • input() reads the entire string: "a b" (and spaces too). - jfs
    • @jfs Yes, your answer is correctly written input is Equivalent to eval(raw_input(prompt)). - c docs.python.org, but the solution works for version 2 - andy.37