Search code examples
pythonpython-2.7units-of-measurementunit-conversion

Is it possible to name a variable 'in' in python?


I am making a unit converter for a homework assignment, and I am using abbreviations for the various units of measurement as variable names, but when I try to define 'in' as a variable, it thinks I mean to use the word 'in' as if I wanted to say 'for x in y' or something like that, and I get the following error:

  File "unit_converter-v1.py", line 13
    in = 12
     ^
  SyntaxError: invalid syntax

Is there any way to get around this?

I know I could just use the word 'inch' or 'inches' but then I would have to type 'inch' or 'inches' into the program every time I needed to convert inches, and I want to make the process as efficient as possible (I'm a senior in highschool, and my phisics teacher will allow us to use unit converters on tests and quizzes if we write the code ourselves. The point is that time is valuable and I need to be as time-efficient as possible).


Solution

  • in is a python keyword, so no you cannot use it as a variable or function or class name.

    See https://docs.python.org/2/reference/lexical_analysis.html#keywords for the list of keywords in Python 2.7.12.