python返回默认语句

python returning to the default statement

嘿,我正在学习 python 困难的事情,我坚持练习 39½。我有以下代码我已经花了很多时间试图找到我的 error/errors 首先,我有一个模块,稍后我将其导入到我的主项目中,如下所示

def new(num_buckets=256):
    """INITIALIZES A map with the given number of buckets."""
    aMap = []
    for i in range(0, num_buckets):
        aMap.append([])
    return aMap

def hash_key(aMap, key):
    """given a key this will create a number and then convert in to an index for the aMaps buckets"""
    return hash(key) % len(aMap)

def get_bucket(aMap, key):
    """given a key find the bucket where it would go"""
    bucket_id = hash_key(aMap, key)
    return aMap[bucket_id]

def get_slot(aMap, key, default=None):
    """retuirn the index , key , nad value of a slot found i an bucket.
    returns -1 key and feult none if not when not found
    """
    bucket = get_bucket(aMap, key)

    for 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 the value in a bucket for the given or the default"""
    i, k, v = get_slot(aMap, key, default=default)
    return v

def set(aMap, key, value):
    """sewts thje key to the valuie replacing any existing valuie"""
    bucket = get_bucket(aMap, key)
    i, k, v = get_slot(aMap, key)
    if i >= 0:
        #the key exists replace it 
        bucket[i] = (key, value)
    else: 
        #the key does not append to create it 
        bucket.append((key, value))

def delete(aMap, key):
    """deltes the given key from the map."""
    bucket = get_bucket(aMap, key)

    for i in xrange(len(bucket)):
        k,v = bucket[i]
        if key == k:
            del bucket[i]
            break

def list(aMap):
    """prints out whats in the map """
    for bucket in aMap: 
        if bucket:
            for k, v in bucket:
                print k,v  

然后是我的主要项目,它看起来像这样

import hashmap

#create a mapping of state to 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 a basic set of states and some cities in them 
cities = hashmap.new()
hashmap.set(cities, "ca", "sanfrancisco")
hashmap.set(cities, "mi", "detroit")
hashmap.set(cities, "fl", "jacksonville")

#add some more cities
hashmap.set(cities, "ny", "new york")
hashmap.set(cities, "or", "portland")

#print some cities
print "_" * 10
print "ny state has: %s" % hashmap.get(cities, "ny")
print "or state has: %s" % hashmap.get(cities, "or")

#print some states
print "-" * 10 
print "michigans abbreviation is %s" % hashmap.get(states, "michigan")
print "floridas abbreviation is: %s" % hashmap.get(states, "florida")

# do it by suing the then 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= with nil result   #can you do this on one line
city = hashmap.get(cities, "tx", "does not exist")
print "the city for the state tx is: %s" % city

由于某些原因我找不到它 returns 当 运行

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

未经测试,只看代码:

if key == kv: 

应该是(第25行左右)

if key == k:

基本上 kv 不存在所以它总是测试 false 并且不 运行 if 语句下的代码。