0

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
>>> 
2
  • You mix input (which you really should, as it opens a large security hole), raw_input and raw_inp, the latter of which does not exist. Also, are you aware that indentation is part of the logic of a Python program? Commented Jan 4, 2012 at 17:10
  • -1: Terrible question title. This appears to be a "variable not defined" question. Please fix your title so other people can learn from it. Commented Jan 4, 2012 at 18:39

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

3 Comments

streetname = dict((y,x) for x,y in streetno.items()) is a great idiom. Thx.
@Finn: Actually in Python >= 2.7, which has dict comprehensions, the idiom is {y:x for x,y in d.iteritems()} (only items in Python 3). Suprisingly, the following is even faster in Python 2.7 (but not in Python 3, it seems!): {d[k]:k for k in d}
even slicker, but both unequivocally better than using zip with unpacking which is what I was fumbling around w/.
2

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.

5 Comments

I think the OP wants the program to continue indefinitely, hence the infinite "while True" loop.
How about this? inp=input('Enter M/N:')
Yes.. I want to the program to continue indefinitely.
@Kiran: You can't use input here. You need raw_input.
You need to indent everything after the while to make the whole thing loop. As it stands (or as it looks here) it'll just keep asking for input for M/N
2

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/

Comments

1

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.

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.