I'm new to the dictionary concept and am stuck at a problem. I have compiled a dictionary for a bookstore, and within the dictionary the key is the author's last and first name, ie. 'Shakespeare,William'.
{'Dickens,Charles': [['Hard Times', '7', '27.00']],
'Shakespeare,William': [['Rome And Juliet', '5', '5.99'],
['Macbeth', '3', '7.99']]}
Values are: book name, quantity on hand, and price. I want a function that can change the quantity of the book.
The user will enter: the author's last name, then first, and then the name of the book, and then the new quantity they want.
If the author does not exist it should say that there isn't an author by the name given, and same thing if the book doesn't exist.
In a separate function, I need to add up the total quantity of this inventory. So as seen right now, it would be 5+3+7 = 15 books. I need a similar function for the price, but it should be essentially the same as that of the quantity I believe.
Thank you for your help.
I tried creating another dictionary with the books as the keys as follow:
def addBook(theInventory):
d = {}
first = input("Enter the first name: ")
last = input("Enter the last name: ")
first = first[0].upper() + first[1:].lower()
last = last[0].upper() + last[1:].lower()
name = last + "," + first
book = input("Enter the name of the book: ")
for name, books in sorted(theInventory.items()):
for title, qty, price in sorted(books):
d[title] = []
d[title].append(qty)
d[title].append(price)
d[book][0] = qty
I need to update theInventory with the new quantity, so theInventory would change in main(), but this isn't doing it. How can I make it so that d is referencing theInventory and changing the qty in there?
I think I came up with something like what you want. One problem I ran into was how you formatted your dictionary. In your original post, you had double lists for all dictionary values. I think it would be easier to format the dictionary like I did. One change I made to keep in mind is that in the changeQuantity() function, I switched the inventory number from a string to an int value. I'm not sure how you want to it be, but the format can easily be changed by making the newquant arg a string type. Hope this helped!
bookdict = {'Dickens,Charles': ['Hard Times', '7', '27.00'],
'Shakespeare,William': [['Rome And Juliet', '5', '5.99'], ['Macbeth', '3', '7.99']]}
def changeQuantity(authorlast,authorfirst,bookname,newquant):
bookfound = False
author = str(authorlast)+','+str(authorfirst)
if not author in bookdict:
return "Author not in inventory"
temp = bookdict.values()
if type(bookdict[author][0]) == list:
for entry in bookdict[author]:
if entry[0] == bookname:
entry[1] = newquant
bookfound = True
else:
if bookdict[author][0] == bookname:
bookdict[author][1] = newquant
bookfound = True
if bookfound == False:
return "Book not in author inventory"
return bookdict
def sumInventory():
sum = 0
for key in bookdict.keys():
if type(bookdict[key][0]) == list:
for entry in bookdict[key]:
sum += int(entry[1])
else:
sum += int(bookdict[key][1])
return sum
print changeQuantity("Dickens","Charles","Hard Times",2)
print changeQuantity("a","b","Hard Times",2)
print changeQuantity("Shakespeare", "William", "a", 7)
print sumInventory()
Output:
{'Shakespeare,William': [['Rome And Juliet', '5', '5.99'], ['Macbeth', '3', '7.99']], 'Dickens,Charles': ['Hard Times', 2, '27.00']}
Author not in inventory
Book not in author inventory
10
Related
in case it isn't already obvious im new to python so if the answers could explain like im 5 years old that would be hugely appreirecated.
I'm basically trying to prove to myself that I can apply some of the basic that I have learnt into making a mini-contact book app. I don't want the data to save after the application has closed or anything like that. Just input your name, phone number and the city you live in. Once multiple names are inputted you can input a specific name to have their information printed back to you.
This is what I have so far:
Name = input("enter name here: ")
Number = input("enter phone number here: ")
City = input("enter city here: ")
User = list((Name, Number, City))
This, worked fine for the job of giving python the data. I made another input that made python print the information back to me just to make sure python was doing what I wanted it to:
print("Thank you! \nWould you like me to read your details back to you?")
bck = input("Y / N")
if bck == "Y":
print(User)
print("Thank you! Goodbye")
else:
print("Goodbye!")
The output of this, is the list that the user creates through the three inputs. Which is great! I'm happy that I have managed to make it function so far;
But I want the 'Name' input to be what names the 'User' list. This way, if I ask the user to input a name, that name will be used to find the list and print it.
How do I assign the input from Name to ALSO be what the currently named "User" list
You will need to create a variable which can store multiple contacts inside of it. Each contact will be a list (or a tuple. Here I have used a tuple, but it doesn't matter much either way).
For this you could use a list of lists, but a dictionary will be more suitable in this case.
What is a dictionary?
A dictionary is just like a list, except that you can give each of the elements a name. This name is called a "key", and it will most commonly be a string. This is perfect for this use case, as we want to be able to store the name of each contact.
Each value within the dictionary can be whatever you want - in this case, it will be storing a list/tuple containing information about a user.
To create a dictionary, you use curly brackets:
empty_dictionary = {}
dictionary_with_stuff_in_it = {
"key1": "value1",
"key2": "value2"
}
To get an item from a dictionary, you index it with square brackets, putting a key inside the square brackets:
print(dictionary_with_stuff_in_it["key1"]) # Prints "value1"
You can also set an item / add a new item to a dictionary like so:
empty_dictionary["a"] = 1
print(empty_dictionary["a"]) # Prints 1
How to use a dictionary here
At the start of the code, you should create an empty dictionary, then as input is received, you should add to the dictionary.
Here is the code I made, in which I have used a while loop to continue receiving input until the user wants to exit:
contacts = {}
msg = "Would you like to: \n - n: Enter a new contact \n - g: Get details for an existing contact \n - e: Exit \nPlease type n, g, or e: \n"
action = input(msg)
while action != "e":
if action == "n": # Enter a new contact
name = input("Enter name here: ")
number = input("Enter phone number here: ")
city = input("Enter city here: ")
contacts[name] = (number, city)
print("Contact saved! \n")
action = input(msg)
elif action == "g": # Get details for an existing contact
name = input("Enter name here: ")
try:
number, city = contacts[name] # Get that contact's information from the dictionary, and store it into the number and city variables
print("Number:", number)
print("City:", city)
print()
except KeyError: # If the contact does not exist, a KeyError will be raised
print("Could not find a contact with that name. \n")
action = input(msg)
else:
action = input("Oops, you did not enter a valid action. Please type n, g, or e: ")
#can be easier to use with a dictionary
#but its just basic
#main list storing all the contacts
Contact=[]
#takes length of contact list,'int' just change input from string to integer
contact_lenght=int(input('enter lenght for contact'))
print("enter contacts:-")
#using for loop to add contacts
for i in range(0,len(contact_lenght)):
#contact no.
print("contact",i+1)
Name=input('enter name:')
Number=input('enter number:')
City=input("enter city:")
#adding contact to contact list using .append(obj)
Contact.append((Name,Number,City))
#we can directly take input from user using input()
bck=input("Thank you! \nWould you like me to read your details back to you?[y/n]:")
#checking if user wants to read back
if bck=='y':
u=input("enter your name:")
#using for loop to read contacts
for i in range(0,len(Contact)):
#if user name is same as contact name then print contact details
if u==Contact[i][0]:
print("your number is",Contact[i][1])
print("your city is",Contact[i][2])
else:
#if user doesnt want to read back then print thank you
print("Good bye")
For this purpose you should use a dictionary.
The key of every entry should be the string 'User[0]' that corresponds to the person's name.
The contents of every entry should be the list with the information of that user.
I'll give you an example:
# first we need to create an empty dictionary
data = {}
# in your code when you want to store information into
# the dictionary you should do like this
user_name = User[0] # this is a string
data[user_name] = User # the list with the information
If you want to access the information of one person you should do like this:
# user_you_want string with user name you want the information
data[user_you_want]
Also you can remove information with this command:
del data[user_you_want_to_delete]
You can get more information on dictionaries here: https://docs.python.org/3/tutorial/datastructures.html#dictionaries
You should start by defining a class to support name, phone and city. Once you've done that, everything else is easy.
class Data:
def __init__(self, name, city, phone):
self.name = name
self.city = city
self.phone = phone
def __eq__(self, other):
if isinstance(other, str):
return self.name == other
if isinstance(name, type(self)):
return self.name == other.name and self.city == other.city and self.phone == other.phone
return False
def __str__(self):
return f'Name={self.name}, City={self.city}, Phone={self.phone}'
DataList = []
while (name := input('Name (return to finish): ')):
city = input('City: ')
phone = input('Phone: ')
DataList.append(Data(name, city, phone))
while (name := input('Enter name to search (return to finish): ')):
try:
print(DataList[DataList.index(name)])
except ValueError:
print('Not found')
Edit: changing code as the issue came up first somewhere else in my code
I'm trying to figure out how to use my dictionary from one function in another one and the other answers on here haven't helped.
The overall goal is to print the key and value just entered into the dictionary named contacts but contacts can only be defined inside init_phonebook
def init_phonebook():
contacts = {}
while True:
choice = print_menu()
if choice == 1:
list_contacts(contacts)
elif choice == 2:
add_contact(contacts)
elif choice == 3:
find_contact(contacts)
elif choice == 4:
edit_contact(contacts)
elif choice == 5:
delete_contact(contacts)
elif choice == 6:
delete_all(contacts)
elif choice == 7:
thanks()
Down here the issue is that .keys and .values don't work as it's not recognizing contacts as a dictionary. I've tried changing the parameter to something else but it doesn't work that way either.
def add_contact(contacts):
phone_number = input('Please enter a phone number: ')
name = input('What would you like to save the name as?: ')
if name in contacts:
print('This contact already exists in your phonebook')
elif name not in contacts:
contacts[name] = phone_number
print('Contact successfully saved')
print('Your updated phonebook is shown below')
for c in contacts:
print('{} --> {}'.format(contacts.keys(c), contacts.values(c)))
print()
the error message I get is:
File "c:\Users\myname\Desktop\VS Code Projects\contact_list.py", line 54, in add_contact
print('{} --> {}'.format(contacts.keys(c), contacts.values(c)))
TypeError: dict.keys() takes no arguments (1 given)
The problem is not how you go about passing the dictionary to these other functions.The error actually arising within your add_contact() function, due to how you are trying to iterate through the dictionary key-val pairs:
for c in contacts:
print('{} --> {}'.format(contacts.keys(c), contacts.values(c)))
It seems you want to iterate through the contacts. contact.keys() does not let you access/index a key, this method returns all of the keys in a dict. contact.values does not let you access/index a value, it returns all of the values in the dict. When you call "for c in contacts", c represents a key. So here are two alternative ways to iterate through:
Properly indexing on the contacts dict using the key c:
for c in contacts:
print('{} --> {}'.format(c, contacts[c]))
By iterating through both the key and value pairs:
for key,value in contacts.items():
print('{} --> {}'.format(key, value))
If confused about a type in Python, I recommend referring to the documentation!
dict.keys() and dict.values() both return arrays that can be indexed but accepts no inputs when called.
But in your case, since you are looping over the dictionary, each iteration stores the key in c. In your case that will be the name associated with the contact.
So you already have the key stored in c on each iteration. What you need next is to use the key to get the value like so dict[key] or dict.get(key).
Alternatively, you can loop over both keys and values simultaneously like so:
for name, phone_number in contacts:
print("{} ==> {}".format(name, phone_number))
I am altering your original code as follows:
def add_contact(contacts):
phone_number = input('Please enter a phone number: ')
name = input('What would you like to save the name as?: ')
if name in contacts:
print('This contact already exists in your phonebook')
else: #You seem to have only two options so no need for elif
contacts[name] = phone_number
print('Contact successfully saved')
print('Your updated phonebook is shown below')
for c in contacts: #c will hold name in each iteration
print('{} --> {}'.format(c, contacts[c]))
I need to create a function that creates a session that accepts a "clerk's" input data about a customer's orders until the "clerk" enters the string "/". Each line of input consists of two elements: the product code and the quantity. Lines of input are formatted as follows: "{product_code},{quantity}". The function should write a file called receipt.txt that prints a summarized report of the session.
The receipt should provide a summary of all the orders made during the session and the product must only appear once if it has been ordered at least once during the session, even if it has been ordered multiple times. In other words, if a product is ordered multiple times, then it should only have one entry in the receipt that describes the sum of all of the orders made for that product. The products must appear in alphabetical order.
Here is my code right now and it prints a receipt but I don't know how to make the order appear only once and make it alphabetical order. Please help.
EDIT: Added get_property function.
def get_property(code,property):
return products[code][property]
def main():
products = {
"americano":{"name":"Americano","price":150.00},
"brewedcoffee":{"name":"Brewed Coffee","price":110.00},
"cappuccino":{"name":"Cappuccino","price":170.00},
"dalgona":{"name":"Dalgona","price":170.00},
"espresso":{"name":"Espresso","price":140.00},
"frappuccino":{"name":"Frappuccino","price":170.00},
}
orders_list = []
total = 0
while(True):
customer_order = input("Welcome to the CoffeePython POS Terminal.\nPlease enter the Product Code and the Quantity in this format - {Product Code},{Quantity}.\nEnter '/' to quit.\n")
if customer_order == "/":
break
else:
code_quantity_list = customer_order.split(",")
code = code_quantity_list[0]
quantity = code_quantity_list[1]
quantity_int = int(quantity)
if code in products:
subtotal = get_property(code,"price")*quantity_int
total += subtotal
ordered_item = dict([
('code', code),
('qty', quantity_int),
('subtotal', subtotal)
])
orders_list.append(ordered_item)
else:
print("The Product Code that you entered is invalid. Please try again.")
print("==")
print("CODE\t\t\tNAME\t\t\tQUANTITY\t\t\tSUBTOTAL")
for order in orders_list:
order_code = order['code']
order_name = products[order_code]["name"]
order_qty = order['qty']
order_subtotal = order['subtotal']
print(f"{order_code}\t\t{order_name}\t\t{order_qty}\t\t\t\t{order_subtotal}\t\t")
print(f"\nTotal:\t\t\t\t\t\t\t\t\t\t{total}")
print("==")
print("Thank you for ordering. Goodbye.")
main()
Output
==
CODE NAME QUANTITY SUBTOTAL
americano Americano 2 300.0
americano Americano 2 300.0
Total: 600.0
==
To store the orders, I would suggest you to use a dictionary with code as a key, and the price as value.
orders_list = {}
while ...:
orders_list[code] = orders_list.setdefault(code, 0) + subtotal
for product in sorted(orders_list):
subtotal = orders_list[product]
print(f"{product:<10} {subtotal}")
You need to check the saved list in orders_list and then evaluate the existing key. For sort list in the order_list by key in a dict, you can use this reference
How do I sort a list of dictionaries by a value of the dictionary?
I am adding a new method to perform check.
Also, I am not sure about your get_property() method, I changed it a bit.
def check_code(orders_list, code):
codes = []
for i in orders_list:
codes.append(i["code"])
if(code in codes):
return True, codes.index(code)
else:
return False, 0
def main():
products = {
"americano":{"name":"Americano","price":150.00},
"brewedcoffee":{"name":"Brewed Coffee","price":110.00},
"cappuccino":{"name":"Cappuccino","price":170.00},
"dalgona":{"name":"Dalgona","price":170.00},
"espresso":{"name":"Espresso","price":140.00},
"frappuccino":{"name":"Frappuccino","price":170.00},
}
orders_list = []
total = 0
while(True):
customer_order = input("Welcome to the CoffeePython POS Terminal.\nPlease enter the Product Code and the Quantity in this format - {Product Code},{Quantity}.\nEnter '/' to quit.\n")
if customer_order == "/":
break
else:
code_quantity_list = customer_order.split(",")
code = code_quantity_list[0]
quantity = code_quantity_list[1]
quantity_int = int(quantity)
if code in products:
# subtotal = get_property(code,"price")*quantity_int
subtotal = products[code]["price"] *quantity_int
check = check_code(orders_list, code)
if check[0]:
orders_list[check[1]]["subtotal"] += subtotal
orders_list[check[1]]["qty"] += quantity_int
else:
ordered_item = dict([
('code', code),
('qty', quantity_int),
('subtotal', subtotal)
])
orders_list.append(ordered_item)
total += subtotal
else:
print("The Product Code that you entered is invalid. Please try again.")
print("==")
print("CODE\t\t\tNAME\t\t\tQUANTITY\t\t\tSUBTOTAL")
orders_list = sorted(orders_list, key=lambda k: k['code'])
for order in orders_list:
order_code = order['code']
order_name = products[order_code]["name"]
order_qty = order['qty']
order_subtotal = order['subtotal']
print(f"{order_code}\t\t{order_name}\t\t{order_qty}\t\t\t\t{order_subtotal}\t\t")
print(f"\nTotal:\t\t\t\t\t\t\t\t\t\t{total}")
print("==")
print("Thank you for ordering. Goodbye.")
main()
When i test the code the item prints once and i did following:
frappuccino, 1
americano, 2
dalgona, 1
So i cannot reproduce the issue but i dont have the get_properties method either so maybe thats the issue.
As for printing the list in alphabetical order you should look to sort the list before looping and printing the reciept. You will find how you can achieve sort on a list containing dictionaries
here
I'm trying to sort the dep_list, which is a list of dictionaries containing employee information (name, department, position, salary). Right now I believe I have it sorting by first name, however I want to sort by last name. If at all possible without breaking the 'name' up into 2 different strings.
#Function for adding employee information
def add_emp():
#Ask the user to add an employee
print("Enter the employee's information:\n")
#Input first and last name
name = str(input("What is the employee's name? ")).title()
#Input employee position
position = str(input("What is their position? ")).title()
#Input employee department
em_department = str(input("What is their department? ")).title()
#Make sure the salary is numeric
try:
#Input employee salary
salary = round(float(input("What is their salary? ")), 2)
#Add information to a dictionary called employees
employees[name] = {"name": name, "position": position, "em_department": em_department, "salary": salary}
except:
print("Salaries must be numeric, silly!")
#Function for adding employees to dictionary by department
def dep_emp():
#Go through all department names stored in the tuple
for x in dep_tup:
#Initialize department list each time to ensure correct sorting
dep_list = []
#Go through all employee dictionaries; when matched, add to the list associated with the corresponding key in the dep_dict dictionary
for names in employees:
if x == employees[names]["em_department"]:
dep_list.append(employees[names])
dep_list.sort(key=operator.itemgetter('name'))
dep_dict[x] = dep_list
continue
Note: The list of dictionaries looks like this:
{
department1: [{'name': name, 'em_department': department, 'position': position, 'salary': salary}, ...],
department2: [...]
}
dep_list.sort(key=lambda x: x['name'].split()[-1])
For each dictionary in dep_list, find the value associated with the name key, split it, and sort based on the last string in the split (should be the last name).
def lists(): #Where list is stored
List = ["Movie_Name",[""],"Movie_Stars",[""],"Movie_Budget",[""]]
print ("Your Movies")
amount_in_list = int(input("How many Movies? "))
x = 1
while x <= amount_in_list:
film = input ("Name of film ... ")
stars = input ("Main stars ...")
Budget = input ("Budget ...")
List.append["Movie_Name"](film)
List.append["Movie_Stars"](stars)
List.append["Movie_Budget"](Budget)
lists()
How do i add the film you enter to the list under the subsetting Movie_Name etc?
A better answer than one which answers your question directly would be: You don't. You definitely need a dictionary for this situation (unless your program develops to a point where you'd prefer creating a custom object)
As a simple demonstration:
def getMovies():
movieinfo = {"Movie_Name": [], "Movie_Stars": [], "Movie_Budget": []}
print ("Your Movies")
amount_in_list = int(input("How many Movies? "))
x = 1
while x <= amount_in_list:
film = input ("Name of film ... ")
stars = input ("Main stars ...")
budget = input ("Budget ...")
movieinfo["Movie_Name"].append(film)
movieinfo["Movie_Stars"].append(stars)
movieinfo["Movie_Budget"].append(budget)
x+=1
return movieInfo
Notice that with a dict you simply use the key string to get the corresponding list (initialized at the start of the function) and append the data as desired.
Edited to provide further information for OP's updated request.
If you want to find a movie's info based on just the movie's name given by the user, you could try something like this:
film = 'The Matrix' # Assuming this is the user's input.
Try:
# The index method will throw an exception if
# the movie cannot be found. If that happens,
# the 'except' clause will execute and print
# the relevant statement.
mIdx = movieinfo['Movie_Name'].index(film)
print '{0} stars {1} and had a reported budget of {2}'.format(
film, movieInfo['Movie_Stars'][mIdx], movieInfo['Movie_Budget'][mIdx])
except ValueError:
print '{0} is not in the movie archives. Try another?'.format(film)
Output:
'The Matrix stars Keanu Reeves and had a reported budget of $80 million'
Or:
'The Matrix is not in the movie archives. Try another?'
I would store the movie information in an object. This way your code will be easier to extend, make changes and reuse. you could easily add methods to your movie class to do custom stuff or add more properties without having to change your code to much.
class Movie:
def __init__(self, name='', actors=[], rating=0 budget=0):
self.name=name
self.actors=actors
self.budget=budget
self.rating=rating
def setName(self, newname):
self.name=newname
def setActors(self, newstars):
self.actors=newstars
def setBudget(self, newbudget):
self.budget=newbudget
def setRating(self, newrating):
self.rating=newrating
# example
mymovies=[]
movie1= Movie('Interstellar',['actor1','actor2','actor3'], 5, 100000)
movie2=Movie()
movie2.setName('other movie')
movie2.setActors(['actor1','actor2','actor3'])
movie2.setBudget(10000)
mymovies.append(movie1)
mymovies.append(movie2)
# or append to your list in a loop