0

This is my test code, test.py:

str = input("IP: ")                                               
print(str) 

When running I get this error:

➜  PingScript git:(master) ✗ python test.py                      
IP: 1.1.1.1 


Traceback (most recent call last):                      
  File "test.py", line 1, in <module>                            
    str = input("IP: ")                                          
  File "<string>", line 1                                         
    1.1.1.1                                                      
        ^                                                 
SyntaxError: invalid syntax 

This happens only if I use 1.1.1.1 as input and not if I use 1.1 as input, what is happening here? I've tried to parse it with str(str) but I still get the same error.

1
  • 3
    Don't name a variable str. What do you expect str(str) to do? Commented Nov 13, 2012 at 12:30

3 Answers 3

7

input() tries to parse the input as a python expression. Use raw_input() instead.

It works with 1.1 as input because that's a literal float value, a number, in python.

It could be that you are following a tutorial meant for Python 3, but are using Python 2 instead. In Python 3, the raw_input() function was renamed to input() and the old input() function was removed altogether. If so, please install Python 3 and continue from there.

Sign up to request clarification or add additional context in comments.

2 Comments

He's using parenthesis for print. Do you think perhaps he's targeting Python 3 but using Python 2?
@StevenRumbalski: Excellent point, and I think you are right. Updated.
1

input() evaluates what you enter as if it were valid Python code. raw_input(), accepts the input and returns it as a string. Since 1.1.1.1 is not valid Python code, you get the error. 1.1 is a float in Python, which is why it seems to work:

>>> input('Enter: ')
Enter: 1+1
2
>>> raw_input('Enter: ')
Enter: 1+1
'1+1'
>>> input('Enter: ')
Enter: 1.1
1.1000000000000001

Comments

0

str() is an inbuilt function in Python language which converts other data types to a string. So str must be a keyword, which cannot be used as a variable name. Kindly use any other variable name instead of "str".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.