''' Purpose: demonstrate while loop and dict mappings ''' # get access to url support import url # where is the dog to notoriety mappings PUPPIES_CSV = 'http://www.cs.virginia.edu/~cs1112/datasets/csv/puppies.csv' # get csv contents database = url.get_csv_dataset( PUPPIES_CSV ) # what should database be? # get data set header = database[ 0 ] # what should header be? what does it represent? dataset = database[ 1 : ] # what should dataset be? what does it represent? # print( header ) # print( dataset ) # print( ) # create a dictionary that helps us find out what a dog "means" # i.e. what fame the dog is associated with dog_to_fame = {} for row in dataset: # what is row? # want to grab 2 pieces of information based on the header dog, fame = row # why don't we need to split the row? ask yourself: is row a string? # print( row ) # we have the information, so let's add this association to our dictionary dog_to_fame[ dog ] = fame # this line maps the fame to the dog and adds this mapping to our dictionary # we could have done this with lists and matching indices, but dictionaries make this process easier # print( dog_to_fame ) # how does dataset differ from dog_to_fame? # reply = input( "Enter a dog name: " ) # what happens when we enter Odie? # name = reply.strip() # fame = dog_to_fame.get( name ) # this line more robust # instead of giving back an error if we don't know about a key, # it compiles and gives back None # fame = dog_to_fame[ name ] # how does this line differ syntactically and semantically # from dog_to_fame[ dog ] = fame? # d[ k ] = v assigns v to be associated with v # v = d[ k ] stores the value of k in v # print( fame ) # repeatedly get dog names until there are no more # the word "until" tends to mean we want to use a while loop looking_for_dog_names = True while ( looking_for_dog_names == True ): # could just say while ( looking_for_dog_names ): reply = input( "Enter a dog name: " ) name = reply.strip() fame = dog_to_fame.get( name ) if ( name == '' ): # if the user entered the empty string, they are no longer looking for dog names # == tells us if what is on the left is the same as what is on the right looking_for_dog_names = False elif ( name not in dog_to_fame.keys() ): # if the dog is not in our keys, print nothing # the in keyword tells us if an element is contained in some kind of sequence or collection print() else: # there should be a fame, print the fame print( fame ) # you could delineate or express these conditions in a lot of ways; # so long as you are changing the condition of the while loop at some point and are # returning the appropriate output, you should be good to go