list - python returning to the default statement -


hey im doing learn python hard way thing , im stuck @ exercise 39½. have following code have been going through lot of time trying find error/errors first have module later import main project looks this

def new(num_buckets=256):     """initializes map given number of buckets."""     amap = []     in range(0, num_buckets):         amap.append([])     return amap  def hash_key(amap, key):     """given key create number , convert in index amaps buckets"""     return hash(key) % len(amap)  def get_bucket(amap, key):     """given key find bucket go"""     bucket_id = hash_key(amap, key)     return amap[bucket_id]  def get_slot(amap, key, default=none):     """retuirn index , key , nad value of slot found bucket.     returns -1 key , feult none if not when not found     """     bucket = get_bucket(amap, key)      i, kv in enumerate(bucket):         k, v = kv         if key == kv:              return i, k, v      return -1, key, default  def get(amap, key, default=none):     """gives value in bucket given or default"""     i, k, v = get_slot(amap, key, default=default)     return v  def set(amap, key, value):     """sewts thje key valuie replacing existing valuie"""     bucket = get_bucket(amap, key)     i, k, v = get_slot(amap, key)     if >= 0:         #the key exists replace          bucket[i] = (key, value)     else:          #the key not append create          bucket.append((key, value))  def delete(amap, key):     """deltes given key map."""     bucket = get_bucket(amap, key)      in xrange(len(bucket)):         k,v = bucket[i]         if key == k:             del bucket[i]             break  def list(amap):     """prints out whats in map """     bucket in amap:          if bucket:             k, v in bucket:                 print k,v   

and main projekt looks this

import hashmap  #create mapping of state abrreviation  states = hashmap.new() hashmap.set(states, 'oregon', 'or') hashmap.set(states, 'florida', 'fl') hashmap.set(states, "california", "ca") hashmap.set(states, "new york", "ny") hashmap.set(states, "michigan", "mi")  #create basic set of states , cities in them  cities = hashmap.new() hashmap.set(cities, "ca", "sanfrancisco") hashmap.set(cities, "mi", "detroit") hashmap.set(cities, "fl", "jacksonville")  #add more cities hashmap.set(cities, "ny", "new york") hashmap.set(cities, "or", "portland")  #print cities print "_" * 10 print "ny state has: %s" % hashmap.get(cities, "ny") print "or state has: %s" % hashmap.get(cities, "or")  #print states print "-" * 10  print "michigans abbreviation %s" % hashmap.get(states, "michigan") print "floridas abbreviation is: %s" % hashmap.get(states, "florida")  # suing cities dict print "-" * 10 print "michigan has: %s" % hashmap.get(cities, hashmap.get(states, "michigan")) print "florida has: %s" % hashmap.get(cities, hashmap.get(states, "florida"))  #print every state abbreviation print "-" * 10 hashmap.list(states)  # print every city in state print "-" * 10  hashmap.list(cities)  print "-" * 10  state = hashmap.get(states, "texas")  if not state:     print "sorry no texas."  #default values using ii= nil result   #can on 1 line city = hashmap.get(cities, "tx", "does not exist") print "the city state tx is: %s" % city 

for reason cannot find returns when run

ny state has: none or state has: none ---------- michigans abbreviation none floridas abbreviation is: none ---------- michigan has: none florida has: none 

with out testing, looking @ code:

if key == kv:  

should (around line 25)

if key == k: 

basically kv doesn't exist tests false , doesn't run code under if statement.


Comments