The error message does not show you the brackets () , so the error in the input() function. You can verify this by calling input() on a separate line.
The fact that you received a SyntaxError by calling the input() function indicates that you are running the code using Python 2, and not Python 3, despite what is indicated in the question marks. In Python 2, input() itself works by analogy with eval(raw_input()) .
You get the SyntaxError because to create a tuple of two numbers, you need to specify a comma . In this case, brackets are completely unnecessary if you do not want to create an empty tuple: () .
Here is the minimal change to your code in question so that it “prints a tuple of all these numbers” (but before using the example, read the section below on the “eval () evil” ):
#!/usr/bin/env python2 print input("Enter 2 comma-separated numbers: ")
or
#!/usr/bin/env python3 print(eval(input("Enter 2 comma-separated numbers: ")))
If you enter:
2,16
then in response both examples will print:
(2, 16)
eval () - evil
Of course, the program user is free to specify __import__('os').remove('важный файл') instead of numbers. Therefore, a safer way is to read the line and recognize the specified input format manually, for example:
#!/usr/bin/env python2 print tuple(map(int, raw_input("Enter 2 comma-separated numbers: ").split(',')))
Or
#!/usr/bin/env python3 print(tuple(map(int, input("Enter 2 comma-separated numbers: ").split(','))))
The results are the same as for the previous examples.
Accepting input from the keyboard, you should expect input errors, so it’s good to catch exceptions and try again to get numbers by typing an informative error message. See the user feedback for a valid response .
(2 16)is actually not a valid syntax in Python. - andreymalx = tuple(raw_input('prompt >> ').split())- andy.37