[英]How can i use input().split() effectively to take at least 2 inputs from user in one line?
[英]How can i use an array to take user input
然后我需要從用戶那里請求 3 個項目,
我嘗試使用 6 個字符串(因此 3 個用於產品,3 個用於價格),因此我可以分別調用每個值。 但后來有人告訴我,數組會大大縮短代碼並將其整理好,所以我嘗試了這樣做,但我正在努力將其調用。 我知道如何計算平均值和總數,但我在數組部分苦苦掙扎。
products = []
products = input("Please enter three shopping products: ")
然后打印代碼並允許我輸入 3 個產品,如下所示: 請輸入三個購物產品:洗發水、肥皂、面包(我仍然需要通過數組詢問每個產品的價格)
shopping = products[0]
print(shopping)
S
我嘗試的第一種方法是 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: ")
它打印問題並允許我輸入,但看起來不太整潔。 我現在需要計算價格的平均值以及總價(我可以在沒有數組的情況下進行計算,但如果我使用數組則令人困惑)我希望我的最終結果是:
[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)
您可以將輸入拆分為一個數組,這將大大縮短代碼。
products = input("Please enter three shopping products (Seperated by comma): ")
result = [x.strip() for x in products.split(',')]
這將去除一串空格,但將它們放入一個數組中。
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))))
輸出:
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
編輯:
如果購物項目是獨一無二的,我建議使用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()))))
輸出:
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.