-1

I used dictionaries here:

percentage = {'a':75.5, 'b':73, 'c':78, 'd':68, 'e':68}
name = input("enter a name: ")
if percentage.has_key(name):
 print dict[name]
else:
 print "no data"

when i've given a name(for example 'a') ,it is showing "unhandled Nameerror -name 'a' is not defined" .whats the solution for it. thank you.

lucky
  • 39
  • 8
  • Are you really using `dict[name]` instead of `percentage[name]`? – TigerhawkT3 Dec 23 '15 at 03:55
  • What Python version are you using? Look this answer: http://stackoverflow.com/a/21122817/4897175 . – antonioduarte Dec 23 '15 at 03:56
  • Assuming you are requesting a key from the correct dictionary, you want to use `get`. `df.get(key)` will return the value from the dictionary if it is present, otherwise it will return None. You can also specify the value to return if the key is not found, e.g. `df.get(key, 'NA')`. – Alexander Dec 23 '15 at 03:58

4 Answers4

2

Are you using Python 2.7 or Python 3?

If you're using 2.7, then you need to use raw_input(), since input() is equivalent to eval(raw_input()), which you probably don't want:

name = raw_input("enter a name: ")

If you're using 3, then print needs to be a function:

print(dict[name])
...
print("no data")

However, common to both versions, some fixing needs to be applied.

First thing is, you don't need to use has_key(). I'm pretty sure this is defunct now, so the most pythonic way to go around this is do:

if name in percentage:

Easy right? Next, you need to print the value, and to do that, the syntax is: dictionary[key] = value. You seem to have it, but you've forgotten to reference your own dictionary and instead used the built-in type dict. It should be:

print percentage[name]

So all together (Python 2.7):

percentage = {'a':75.5, 'b':73, 'c':78, 'd':68, 'e':68}
name = raw_input("enter a name: ")
if percentage.has_key(name):
   print percentage[name]
else:
   print "no data"
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • If the OP was using Python 3 there would be a `SyntaxError` before it could produce a `NameError`. – TigerhawkT3 Dec 23 '15 at 04:00
  • @TigerhawkT3 Ah, didn't know about that. I don't use Python 3 myself – TerryA Dec 23 '15 at 04:01
  • Yep. In older versions of Python 3 it gives a generic traceback, but it was eventually changed to say `SyntaxError: Missing parentheses in call to 'print'`. – TigerhawkT3 Dec 23 '15 at 04:05
1

Try changing to reference the name of your dictionary:

percentage = {'a':75.5, 'b':73, 'c':78, 'd':68, 'e':68}
name = input("enter a name: ")
if percentage.has_key(name):
   print percentage[name]
else:
   print "no data"
Stidgeon
  • 2,673
  • 8
  • 20
  • 28
1

First, you have to refer to your dictionary by its actual name, percentage, not dict. dict is the type and function, not the object you created.

Secondly, there's an easier way to do this, with the get method:

percentage = {'a':75.5, 'b':73, 'c':78, 'd':68, 'e':68}
name = input("enter a name: ")
print percentage.get(name, 'no data')

This will attempt to retrieve the value for name, and if there is no such key it will instead return the string 'no data'.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

Don't use input in python2.7, because it return s a function instead of a string. Use raw_input instead please.