Kiran Bhat
Kiran Bhat

Reputation: 3847

Error in Python

This is my 1st Python program.I implemented a dictionary as shown below. Here I'm trying to retrieve values*(either key or value)* depending upon the user input.

For eg: If user enters "1" then I need to retrieve "Sachin Tendulkar " from the dictioanry. If user enter "Sachin Tendulkar" then I need to retrieve "1" from dictioanry.

streetno={"1":"Sachin Tendulkar","2":"Sehawag","3":"Dravid","4":"Dhoni","5":"Kohli"}
while True:
     inp=input('Enter M/N:')
if inp=="M" or inp=="m":
    key=raw_input( "Enter the main number :")
    result=streetno.get(key)
else:
      key=raw_inp("Enter the street name : ")
        result=streetno.get(key)

I think there's nothing wrong with the logic. But i'm not able to execute it. I'm getting below shown error.

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Enter M/N:m

Traceback (most recent call last):
File "C:\Users\kiran\Desktop\Cricketpur.py", line 3, in <module>
 inp=input('Enter M/N:')
File "<string>", line 1, in <module>
NameError: name 'm' is not defined
>>> 

Upvotes: 0

Views: 398

Answers (4)

Niklas B.
Niklas B.

Reputation: 95348

The problem is within this line:

inp=input('Enter M/N:')

You use input instead of raw_input, which you really shouldn't, as it executes everything the user inputs as Python code. Just use raw_input here and it should be fine.

However, the rest of your code is also broken and shouldn't work as you expect it to work. My attempt at fixing it:

streetno = { "1" : "Sachin Tendulkar",
             "2" : "Sehawag",
             "3" : "Dravid",
             "4" : "Dhoni",
             "5" : "Kohli"}

# we create a "reversed" dictionary here that maps
# names to numbers
streetname = dict((y,x) for x,y in streetno.items())

while True:
    inp = raw_input('Enter M/N:')
    if inp == "M" or inp == "m":
        key = raw_input("Enter the main number:")
        # you don't need .get here, a simple [] is probably what you want
        result = streetno[key]
    else:
        key = raw_input("Enter the street name: ")
        # we need to use our reversed map here!
        result = streetname[key]

    # do something with the result (change that to whatever you want
    # to do with it)
    print result

Upvotes: 5

Makoto
Makoto

Reputation: 106498

Since you're using Python 2.7, input() behaves like eval(raw_input()), which means it's evaluating what's coming in as input. Change that to raw_input(). Also, raw_inp isn't a function so you'd need to change that to raw_input() as well.

There are some indentation issues as well; you should make sure that everything is properly indented. It also seems your loop will not end.

Upvotes: 1

apiguy
apiguy

Reputation: 5362

The problem looks like you are using input for the first question (you are using raw_input for the others)

As a side note, you may want to check out python's built-in module cmd for this kind of program. It's specifically for creating command line programs like this:

http://www.doughellmann.com/PyMOTW/cmd/

Upvotes: 2

Spacedman
Spacedman

Reputation: 94287

If you want a character typed in use raw_input, not input (which takes an expression and tries to evaluate it), and not raw_inp (which doesn't exist).

Also, your 'while True' expression is never going to end.

And you haven't indented your 'else' clause properly.

Upvotes: 2

Related Questions