0% found this document useful (0 votes)
5 views1 page

Python Dictionary and Cost Calculator

The document defines a Dictionary class that allows adding and looking up entries. It also includes a function to calculate the total cost of items including tax and a function to create a new word from a list of words based on their indices. Example usages of these functionalities are provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Python Dictionary and Cost Calculator

The document defines a Dictionary class that allows adding and looking up entries. It also includes a function to calculate the total cost of items including tax and a function to create a new word from a list of words based on their indices. Example usages of these functionalities are provided.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class Dictionary:

elements = {}

def __init__(self):
[Link] = {}

def newentry(self, key, value):


"""Add a new entry to the dictionary."""
[Link][key] = value

def look(self, key):


"""Look up an entry in the dictionary."""
return [Link](key, "Cant find entry for " + key)

d = Dictionary()
[Link]("Apple", "A fruit that is red or green.")
print([Link]("Apple"))
print([Link]("Banana"))

def get_total(costs, items_bought, tax):


subtotal = sum(costs[item] for item in items_bought)
total = subtotal * (1 + tax)
return round(total, 2) # Redondeamos a 2 decimales

costs = {'socks':5, 'shoes':60, 'sweater':30}


print(get_total(costs, ['socks', 'shoes'], 0.09))

def new_word(words):
return ''.join(word[i] for i, word in enumerate(words) if i < len(word))

word = new_word(["yoda", "best", "has"])


print(word) # Output: "ye

You might also like