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"