简体   繁体   English

我如何使用数组来获取用户输入

[英]How can i use an array to take user input

I need to request 3 items from a user then,然后我需要从用户那里请求 3 个项目,

  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打印一个声明说“产品1,产品2,产品3的总和是xxxx,平均值是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.我尝试使用 6 个字符串(因此 3 个用于产品,3 个用于价格),因此我可以分别调用每个值。 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)然后打印代码并允许我输入 3 个产品,如下所示: 请输入三个购物产品:洗发水、肥皂、面包(我仍然需要通过数组询问每个产品的价格)

shopping = products[0]
 print(shopping)
 S

The first way I tried was the 6 strings, like below:我尝试的第一种方法是 6 个字符串,如下所示:

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. [product1], [product2], [product3] 的总价为 Rxx, xx,商品的平均价格为 Rxx, xx。


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)

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.这将去除一串空格,但将它们放入一个数组中。

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 :如果购物项目是独一无二的,我建议使用dict方法,将items作为keys ,将prices作为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
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))    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM