r/PythonProjects2 • u/gobionreddit • Apr 11 '20
PYTHON DICTIONARIES HW HELP!
Hi all hope you are doing well during this unusual time. I am taking programming I class and it has been hard to communicate with my professor during these times so I decided to turn to reddit and ask for some help as I am confused with dictionaries. This is my first assignment with dictionaries. Help is very greatly appreciated!! Thanks in advance
Create a list called fruits_list with the values ‘bananas’, ‘apples’, and ‘grapes’ and a list called fruits_price with the values 0.59, 3.99, and 2.99.
Using a for loop, create a dictionary price_per_pound containing the elements of fruits_list as keys with the corresponding values from fruits_price (so bananas cost $0.59 per pound, apples cost $3.99 per pound, and grapes cost $2.99 per pound)
Create a function groceries(fruits_list) that asks the user for the number of pounds of each fruit type in the input list fruits_list and returns the information in a dictionary called fruit_quantity.
Create a function calculate_bill(dict_price,dict_quantity)that takes two dictionaries dict_price and dict_quantity as input arguments and returns the total value of the bill.
Create a main function to run your functions and print the total price.
1
u/gobionreddit Apr 12 '20
This is my code right now. It has some issues and I am unsure how to fix them.
#part 1
fruits_list = ['bananas', 'apples', 'grapes'] #list with fruit keys
fruits_price = [0.59, 3.99, 2.99] #list with fruit values
#part 2
price_per_pound = {} #setting up dictionary for the 'for' loop
#sets each value from fruit list = to that of fruit price
for eachfruit in fruits_list and fruits_price:
price_per_pound[eachfruit] = fruits_price
#^fruit is the key and it is equal to fruits_price which is the value
#part 3
#asks user for amount of lb for each fruit, then returns user values in the dictionary fruit_quantity
def groceries(fruits_list):
bananas = int(input("How many bananas would you like?"))
apples = int(input("How many apples would you like?"))
grapes = int(input("How many grapes would you like?"))
fruit_quantity = {'bananas': bananas, 'apples': apples, 'grapes': grapes}
return fruit_quantity
#part4
def calculate_bill(dict_price, dict_quantity):
#dictioanry for price per fruit
dict_price = {'bananas': 0.59, 'apples': 3.99, 'grapes': 2.99}
#dictionary for price per pound
dict_quantity = {'bananas': bananas, 'apples': apples, 'grapes': grapes}
#multiplying amount customer wants by price per lb (finding total price)
bill = bananas * dict_price['bananas'] + apples * dict_price['apples'] + grapes * dict_price[grapes]
return bill
#executing everything above
def main():
print("Welcome to the virtual grocery store!")
print(groceries(fruits_list))
print(calculate_bill(dict_price, dict_quantity))
main()
ERROR MESSAGE:
Welcome to the virtual grocery store!
How many bananas would you like?1
How many apples would you like?1
How many grapes would you like?1
{'bananas': 1, 'apples': 1, 'grapes': 1}
Traceback (most recent call last):
File "/Users/ /Desktop/Programming/_dict.py", line 42, in <module>
main()
File "/Users/ /Desktop/Programming/_dict.py", line 41, in main
print(calculate_bill(dict_price, dict_quantity))
NameError: name 'dict_price' is not defined
How can I resolve these errors?