[Tutor] Need help fixing some code for a project
John Cocks
johncocksbusiness at gmail.com
Sun Nov 26 20:57:37 EST 2017
The task is broken down into three sections.
Section 1 - User Input
Section 2 - loop through the grocery list
Section 3 - provide output to the console
'''
#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' :
#Accept input of the name of the grocery item purchased.
item_name = "Item name:\n"
#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")
# Define variable to hold grand total called 'grand_total'
grand_total = 0
#Define a 'for' loop.
for grocery_item in grocery_history:
#Calculate the total cost for the grocery_item.
item_total = number * price
#Add the item_total to the grand_total
item_total = grand_total
#Output the information for the grocery item to match this example:
#2 apple @ $1.49 ea $2.98
print('number', 'name', 'price', 'cost')
#print(name)
#print(price: %6.2f) -- errors happen here
#print(item_total: %6.2f) -- errors happen here too
#Set the item_total equal to 0
item_total = 0
#Print the grand total
print(grand_total)
More information about the Tutor
mailing list