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

4

u/Meorge Apr 12 '20

What have you accomplished/understood of this assignment so far? I'm happy to try to help with clearing up misunderstandings and confusion, but I don't want to simply give answers.

2

u/gobionreddit Apr 12 '20

I am able to ask the user how much fruit they want by saying:

user_fruit = int(input(“How many pounds of bananas would you like?”)) User_fruit2 = int(input(“how many pounds of apples would you like?”)) User_fruit3 = int(input(“how many pounds of grapes would you like?”))

but am unsure how to return the response as a dictionary

1

u/dr_crispin Apr 12 '20

Just an aside from someone who’s written a script or two (but only two, mind you), while things like user_fruit, user_fruit2 etc will do for now, it’s best to get in the habit of making your names more descriptive. So, instead of

user_fruit = int(input(“How many pounds of bananas would you like?”))

You’d end up with something like

bananas_weight_wanted = int(input(“How many pounds of bananas would you like?”))

Only then, well, better. bananas_desired, pounds_of_bananas, user_desired_amount_of_bananas, yellow_thingmajigs_in... okay, well maybe not that last one.

If you’re gonna use user_fruit, or user_variable, or whatnot it’s fine in a small script like this, but if you’re gonna write bigger scripts (and eventually, you will) you’re gonna be left scratching your head as to what on earth you were doing with those.

2

u/gobionreddit Apr 12 '20

I see what you mean. It helps when writing the code as the the variable name is more like an English word so it is easier to remember and use. Thanks for the tip!