[Tutor] Need help fixing some code for a project

Alan Gauld alan.gauld at yahoo.co.uk
Mon Nov 27 04:48:10 EST 2017

On 27/11/17 01:57, John Cocks wrote:
> The task is broken down into three sections.

What exactly do you want help with?
Do you not understand part of the problem?
Do you not know how to code some part?
Are you getting an error? If so show us the full error text.

WE are not mind readers, you need to tell us what
you want to know.

As for the code below, I've added a few remarks...

> #Task: Create the empty data structure
> grocery_item = {}
> grocery_history = []
> #Variable used to check if the while loop condition is met
> stop = 'go'
> while stop == 'go' :

This would read better if stop were a boolean variable

stop = False
while not stop:

>     #Accept input of the name of the grocery item purchased.
>     item_name = "Item name:\n"

Your comment says "accept input" but you are not accepting
input, you are just assigning a string to a variable.
Do you know how to read input from a user?

>     #Accept input of the quantitiy of the grocery item purchased.
>     quantity = "Quantity purchased:\n"
>     #Accept input of the cost of the grocery item input (this is a per-item
> cost).
>     cost = "Price per item:\n"
>     #Create a dictionary entry which contains the name, number and price
> entered by the user.
>     grocery_item = {'name': item_name, 'number': int(quantity), 'price':
> float(cost)}  -- errors usually occur here because of "quantity
> purchased:\n line
>     #Add the grocery_item to the grocery_history list using the append
> function
>     grocery_history.append(grocery_item)
>     #Accept input from the user asking if they have finished entering
> grocery items.
>     print("Would you like to enter another item?\n Type 'c' for continue or
> 'q' to quit:\n")

If you were reading input how would you exit the while loop above?
You need to test for that situation and take the appropriate action.

> # Define variable to hold grand total called 'grand_total'
> grand_total = 0

Its traditional to put ALL the variable definitions at the top
of your code so you know where to find them easily.

> #Define a 'for' loop.
> for grocery_item in grocery_history:

Its probbablyt not a good idea to use the same variable
name for the loop as you did in the input code above.
In this case it won't bite you but it could get
confusing. I'd suggest maybe just use 'item' instead?

>   #Calculate the total cost for the grocery_item.
>   item_total = number * price

You don't have variables called 'number' or 'price'
but you do have dictionary keys in grocery_item.
So you really need:

item_total = grocery_item['number'] * grocery_item['price']

>   #Add the item_total to the grand_total
>   item_total = grand_total

You are not adding you are assigning. And in fact
you are wiping out your total by replacing it with 0!

>   #Output the information for the grocery item to match this example:
>   #2 apple @ $1.49 ea $2.98
>   print('number', 'name', 'price', 'cost')

Her you just print the key strings not the actuial values.
Again you need to use the keys to access the item.

>   #Set the item_total equal to 0
>   item_total = 0
> #Print the grand total
> print(grand_total)

HTH, but, if you have more specific queries, come back to
us with more detail.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list