Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 保存用户输入_Python_Python 3.x_Python 2.7 - Fatal编程技术网

Python 保存用户输入

Python 保存用户输入,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我买卖产品,我想做一个节省时间的程序。 当我购买这些固定产品时,我想通过制作一个程序来节省时间,该程序会问我“x产品的数量”,然后是“y产品”等等。 最后,我希望它能打印出我总共有多少种产品以及它们的美元价值 在两个问题之间,我希望它以美元打印该产品的总额。例如,输入“多少产品X”10输出:“你有10个产品X,价值100美元。” 最后,我想让python把x,y,z加起来,打印“你有x个产品,总共价值300美元” 这就是我到目前为止想到的 product1 = 20 product2 = 1

我买卖产品,我想做一个节省时间的程序。 当我购买这些固定产品时,我想通过制作一个程序来节省时间,该程序会问我“x产品的数量”,然后是“y产品”等等。 最后,我希望它能打印出我总共有多少种产品以及它们的美元价值

在两个问题之间,我希望它以美元打印该产品的总额。例如,输入“多少产品X”10输出:“你有10个产品X,价值100美元。”

最后,我想让python把x,y,z加起来,打印“你有x个产品,总共价值300美元”

这就是我到目前为止想到的

 product1 = 20
 product2 = 12
 product3 = 20
 product4 = 25
 product5 = 25
 product6 = 17
 product7 = 19
 product8 = 19
 product9 = 17
 product10 = 25
 product11 = 5
 product12 = 5
 product13 = 5
 product14 = 20
 product15 = 24

def timesaver():
    product1_amount = int(input("How many product1? "))
    print(f"You have {product1_amount} product1 and  {product1_amount * product1} Dollars worth\n\n")
    product1_total =  product1_amount 
    product1_dollars = product1_amount * product1
    print('\n\n')
我一遍又一遍地重复这一点来完成这项工作——代码太糟糕了,根本没有效率。有什么帮助吗

我还做了一些循环,问这样的问题,把所有的产品放在一个没有附加价格的列表中

但是如何将每个用户输入保存到不同的变量中,以便对每个变量执行操作

for index in products:
    question = int(input("How many " + str(index) + " ?"))

考虑将产品价格存储到一个数组中:

products = [20, 12, 30, 25, 25, 17, 19, 19, 17, 25, 5, 5, 5, 20, 24]
def timesaver() :
    productNumber = 1
    totals = []
    dollars = []
    for product in products :
        amount = int(intput("How many product{}? ".format(productNumber))
        print(f"You have {amount} product{productNumber} and {amount * product} Dollars worth\n\n")
        totals.append(amount)
        dollars.append(amount * product)
        print('\n\n')
     print(totals)
     print(dollars)
现在仍然保存了所有相同的值


例如:
product1\u total
=
totals[0]
product2\u total
=
totals[1]
等。

考虑将产品价格存储到一个数组中:

products = [20, 12, 30, 25, 25, 17, 19, 19, 17, 25, 5, 5, 5, 20, 24]
def timesaver() :
    productNumber = 1
    totals = []
    dollars = []
    for product in products :
        amount = int(intput("How many product{}? ".format(productNumber))
        print(f"You have {amount} product{productNumber} and {amount * product} Dollars worth\n\n")
        totals.append(amount)
        dollars.append(amount * product)
        print('\n\n')
     print(totals)
     print(dollars)
现在仍然保存了所有相同的值


例如:
product1\u total
=
totals[0]
product2\u total
=
totals[1]
等。

根据价格制作产品字典。然后循环浏览该词典,询问用户需要多少特定产品。使用该数据构建表示订单本身的第二个字典

products = {
    "apple": 1.00,
    "pear": .70,
    "banana": .10,
    "kiwi": 1.50,
    "melon": 5.75,
    "pineapple": 12.0,
}


def get_order():
    order = {}
    for product, price in products.items():
        amount = int(input(f"How many {product} do you want at {price:.2f} each?"))
        order[product] = (price, amount)
    print("Your total is {:.2f}".format(sum(x*y for x, y in order.values())))
    return order

根据价格编一本产品词典。然后循环浏览该词典,询问用户需要多少特定产品。使用该数据构建表示订单本身的第二个字典

products = {
    "apple": 1.00,
    "pear": .70,
    "banana": .10,
    "kiwi": 1.50,
    "melon": 5.75,
    "pineapple": 12.0,
}


def get_order():
    order = {}
    for product, price in products.items():
        amount = int(input(f"How many {product} do you want at {price:.2f} each?"))
        order[product] = (price, amount)
    print("Your total is {:.2f}".format(sum(x*y for x, y in order.values())))
    return order

使用函数是一个良好的开端

你可能想考虑使用<代码>字典<代码>。它允许您按名称进行查找,这是一个固定的时间过程,这意味着无论您有多少产品,查找时间都是相同的

可以这样做:

products = {'product1_name': 20, 'product2_name': 12, ..., 'productn_name': 300}
考虑到这一点,您可以对函数执行以下操作:

def timesaver(product, amount):
    # Product is the name of the product you want
    val = products[product]

    # Amount is an integer value
    total = amount * val

    print("Total for product {0}: {1}".format(product, total))
使用您想要的
input
功能,总使用量可以在while循环中:

for product in products.keys():
    amt = int(input("How many of {}?  ".format(product)))
    timesaver(product, amt)

使用函数是一个良好的开端

你可能想考虑使用<代码>字典<代码>。它允许您按名称进行查找,这是一个固定的时间过程,这意味着无论您有多少产品,查找时间都是相同的

可以这样做:

products = {'product1_name': 20, 'product2_name': 12, ..., 'productn_name': 300}
考虑到这一点,您可以对函数执行以下操作:

def timesaver(product, amount):
    # Product is the name of the product you want
    val = products[product]

    # Amount is an integer value
    total = amount * val

    print("Total for product {0}: {1}".format(product, total))
使用您想要的
input
功能,总使用量可以在while循环中:

for product in products.keys():
    amt = int(input("How many of {}?  ".format(product)))
    timesaver(product, amt)

如果是我,一开始我会在Excel中完成。@user1443098:只需编辑打字错误。不需要留下无关的注释。如果是我,一开始我会在Excel中完成。@user1443098:只需编辑打字。没有必要留下无关的评论。如果OP想让名称与他们的产品关联,这肯定比我的答案更好。如果OP想让名称与他们的产品关联,这肯定比我的答案更好。