r/PythonProjects2 • • 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

  1. 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.

  2. 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)

  3. 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​.

  4. 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.

  5. Create a ​main​ function to run your functions and print the total price.

0 Upvotes

12 comments sorted by

View all comments

Show parent comments

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?

1

u/Meorge Apr 12 '20

The issue here is that the variable dict_price is not defined in the scope of your overall program - you give it a value inside of the calculate_bill() function, but outside of that it has no meaning. You either need to remove it from your call to calculate_bill() (and the function’s list of arguments) or you need to define it in the scope of your main() function so that you can pass it the variable (and in this case, you could remove the line where you define it inside of calculate_bill()).

1

u/gobionreddit Apr 12 '20

When I move it to the main function it says that thee variable bananas are not defined

1

u/Meorge Apr 13 '20

Like with dict_price, the scope of the variable bananas is limited to the function you created it in (i.e. the groceries() function). The easiest way to get around this is to declare bananas a global variable by adding global bananas right under the function declaration for each function you use that variable in. This will make it so that all functions including this line will reference the same variable.

As a side note, when typing code in Reddit, having three backticks (like ```) before and after the code block (on their own lines) will make the code easier to read.