0

I am new to python and I am trying to learn the language while also trying to see some differences between python2.7 & python 3.

I understand the fundamental difference between python2.7 and python3 with respect to input from resources like this

Here is what I am trying to do:

relation = {"saurav": { "age":21, "profession" : "student"}, "rini": {"age": 28, "profession" :"singer"}}
print(relation["rini"])
print("___________________")
print(relation["saurav"])
print("**************")
name = str(input("Enter name "))
print(name)
print(relation[name])

and when I run the above code in python3 I get the expected response. But when I run the same code in python 2.7, I get an error as below:

Traceback (most recent call last):
  File "rish.py", line 6, in <module>
    name = str(input("Enter name "))
  File "<string>", line 1, in <module>
NameError: name 'rini' is not defined

This is how my output looks like: enter image description here

Can someone please help me understand why am I getting this error in python2.7 and not in python 3?

halbmond
  • 29
  • 1
  • 5
  • 1
    See e.g. https://www.python.org/dev/peps/pep-3111/; in general, changes between versions are covered in e.g. https://docs.python.org/3/whatsnew/3.0.html. It's confusing that you say you understand the difference, but then ask why you're getting the error. – jonrsharpe May 07 '20 at 14:57
  • 1
    no, you don't understand the difference between python2 and python3. you need to use `raw_input` in python2. And why would you use python2 in the first place. – buran May 07 '20 at 14:58
  • 1
    `input()` does very different things in Python 2 vs Python 3. In Python 2, use `raw_input()` instead. – John Gordon May 07 '20 at 14:58
  • 1
    2.7 tries to execute the input as code. – Michael Butscher May 07 '20 at 14:58
  • 1
    If you are learning the language, make your life easier and go with Python 3. – Thierry Lathuille May 07 '20 at 14:59

0 Answers0