I need to request 3 items from a user then,

  1. Request the price of those three items from a user
  2. Calculate the total of all three
  3. Calculate the average
  4. print a statement saying "the total of product 1, product 2, product 3 is xxxx and the average is xxxx

I've tried to use 6 strings (so 3 for the products and 3 for the prices) so I could call each value separately. But then I was told an array would greatly shorten the code and neaten it up so I tried that but I'm battling to call it forward. I know how to work out the average and total but I'm struggling with the array part.

 products = []
 products = input("Please enter three shopping products: ")

The code then prints and allows me to input the 3 products like below: Please enter three shopping products: Shampoo, Soap, Bread (I still need to ask the prices for each product through an array)

shopping = products[0]
 print(shopping)
 S

The first way I tried was the 6 strings, like below:

product1 = input("Please enter a shopping item: ")
product2 = input("Please enter a second shopping item: ")
product3 = input("Please enter a third shopping item: ")
price1 = input("Please enter the price for the first shopping item: ")
price2 = input("Please enter the price for the second shopping item: ")
price3 = input("Please enter the price for the third shopping item: ")

It prints the question and allows my input, but it doesn't look very neat. I now need to calculate the average of the prices as well as the total (which I can do without the array but if I'm using the array it's confusing) I'm wanting my end result to be:

The Total of [product1], [product2], [product3] is Rxx, xx and the average price of the items are Rxx, xx.


4 Answers

0
samthegolden On Best Solutions
products = []
prices = []
for i in range (0,3):
    product = raw_input("Enter your Item to the List: ")
    products.append(product)
for j in range (0,3):
    price = float(raw_input("Enter your price to the List: "))
    prices.append(price)
print "The total of products is " + str(sum(prices)) + " and the average price is " + str(sum(prices)/3)
0
BloodViolet On

You could split the input into an array which would shorten the code by quite a bit.

products = input("Please enter three shopping products (Seperated by comma): ")
result = [x.strip() for x in products.split(',')]

This would strip the string of spaces and but them into an array.

0
user5173426 On
itemsList = []
priceList = []


for i in range(1,4):
    itemStr = "Enter the {} shopping item: ".format(i)
    itemsList.append(input(itemStr))
    priceStr = "Enter the price of {} shopping item: ".format(itemsList[i-1])
    priceList.append(int(input(priceStr)))

print("The Total of {}, {}, {} is {} and the average price of the items are {}".format(*itemsList, sum(priceList), sum(priceList) / float(len(priceList))))

OUTPUT:

Enter the 1 shopping item: shopping_item_1
Enter the price of shopping_item_1: 1
Enter the 2 shopping item: shopping_item_2
Enter the price of shopping_item_2: 2
Enter the 3 shopping item: shopping_item_3
Enter the price of shopping_item_3: 3
The Total of shopping_item_1, shopping_item_2, shopping_item_3 is 6 and the average price of the items are 2.0

EDIT:

If the shopping items are unique, I'd suggest a dict approach with items as keys and prices as values:

n = int(input("Enter the total items: "))
shoppingDict = {}

for i in range(n):
    keys = input("Enter the shopping item: ")
    values = int(input("Enter the price of item: "))
    shoppingDict[keys] = values

print("The Total of {}, {}, {} is {} and the average price of the items are {}".format(*shoppingDict.keys(), sum(shoppingDict.values()), sum(shoppingDict.values()) / float(sum(shoppingDict.values()))))

OUTPUT:

Enter the total items: 3
Enter the shopping item: shopping_item_1
Enter the price of item: 1
Enter the shopping item: shopping_item_2
Enter the price of item: 2
Enter the shopping item: shopping_item_3
Enter the price of item: 3

The Total of shopping_item_1, shopping_item_2, shopping_item_3 is 6 and the average price of the items are 1.0
0
user1408786 On
class ShoppingItem:
    price = 0
    name = ""

    def __init__(self, name, price):
        self.price = price
        self.name = name


shoppingItems = []

for i in range(0, 3):
    productName = input("Please enter a shopping item: ")
    price = input("Please enter a price for the shopping item: ")
    shoppingItems.append(ShoppingItem(productName, price))


total = sum(i.price for i in shoppingItems)
average = total/len(shoppingItems))