Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x 如何将不同的输入存储在单个变量中并打印出来?_Python 3.x - Fatal编程技术网

Python 3.x 如何将不同的输入存储在单个变量中并打印出来?

Python 3.x 如何将不同的输入存储在单个变量中并打印出来?,python-3.x,Python 3.x,到目前为止,代码可以正常工作并要求我提供所需的一切,但它一直在输出最后的输入,而不是最后的全部3个输入。我知道为什么,但不知道如何修复。谢谢你的帮助 我试过做3个不同的功能,但只是把它弄得一团糟,无法让它工作 class RetailItem: def __items__(s,d,p): return s,d,p def main(): for x in range (3): s = input("What is

到目前为止,代码可以正常工作并要求我提供所需的一切,但它一直在输出最后的输入,而不是最后的全部3个输入。我知道为什么,但不知道如何修复。谢谢你的帮助

我试过做3个不同的功能,但只是把它弄得一团糟,无法让它工作

class RetailItem:

    def __items__(s,d,p):
            return s,d,p
    def main():
        for x in range (3):
            s = input("What is item name: ")
            d = int(input("How many of these items are in stock? "))
            p = float(input("How much is each unit price? "))
        for __items__ in range(3):
            print("Description:", s)
            print("Units:", d)
            print("Price:", p)
    main()
所以现在代码就是这样做的

What is item name: 1
How many of these items are in stock? 1
How much is each unit price? 1
What is item name: 2
How many of these items are in stock? 2
How much is each unit price? 2
What is item name: 3
How many of these items are in stock? 3
How much is each unit price? 3
Description: 3
Units 3
Price 3.0
Description: 3
Units 3
Price 3.0
Description: 3
Units 3
Price 3.0
最后我想让它读出来

Description: 1
Units 1
Price 1
Description: 2
Units 2
Price 2
Description: 3
Units 3
Price 3

在循环的每次迭代中,您都覆盖了变量所持有的先前值

对于您的目标来说,这可能过于复杂,但您可以将字典作为数据结构嵌套在列表中

示例:

#initialize an empty list and dict
myList = []
myDict = {}
def main():
    for x in range (3):
        #each loop overrides the dict
        myDict["description"] = input("What is item name: ")
        myDict["units"] = int(input("How many of these items are in stock? "))
        myDict["price"] = float(input("How much is each unit price? "))
        #append a copy of dict to our list
        myList.append(myDict.copy())
    #loop through list and unpack values from dictionaries
    for item in myList:
        for key, value in item.items():
            print(key.capitalize() + ":" + str(value))

main()
What is item name: 1
How many of these items are in stock? 1
How much is each unit price? 1
What is item name: 2
How many of these items are in stock? 2
How much is each unit price? 2
What is item name: 3
How many of these items are in stock? 3
How much is each unit price? 3
Description:1
Units:1
Price:1.0
Description:2
Units:2
Price:2.0
Description:3
Units:3
Price:3.0
输入:

#initialize an empty list and dict
myList = []
myDict = {}
def main():
    for x in range (3):
        #each loop overrides the dict
        myDict["description"] = input("What is item name: ")
        myDict["units"] = int(input("How many of these items are in stock? "))
        myDict["price"] = float(input("How much is each unit price? "))
        #append a copy of dict to our list
        myList.append(myDict.copy())
    #loop through list and unpack values from dictionaries
    for item in myList:
        for key, value in item.items():
            print(key.capitalize() + ":" + str(value))

main()
What is item name: 1
How many of these items are in stock? 1
How much is each unit price? 1
What is item name: 2
How many of these items are in stock? 2
How much is each unit price? 2
What is item name: 3
How many of these items are in stock? 3
How much is each unit price? 3
Description:1
Units:1
Price:1.0
Description:2
Units:2
Price:2.0
Description:3
Units:3
Price:3.0
输出:

#initialize an empty list and dict
myList = []
myDict = {}
def main():
    for x in range (3):
        #each loop overrides the dict
        myDict["description"] = input("What is item name: ")
        myDict["units"] = int(input("How many of these items are in stock? "))
        myDict["price"] = float(input("How much is each unit price? "))
        #append a copy of dict to our list
        myList.append(myDict.copy())
    #loop through list and unpack values from dictionaries
    for item in myList:
        for key, value in item.items():
            print(key.capitalize() + ":" + str(value))

main()
What is item name: 1
How many of these items are in stock? 1
How much is each unit price? 1
What is item name: 2
How many of these items are in stock? 2
How much is each unit price? 2
What is item name: 3
How many of these items are in stock? 3
How much is each unit price? 3
Description:1
Units:1
Price:1.0
Description:2
Units:2
Price:2.0
Description:3
Units:3
Price:3.0
也许有一种更简单的方法来完成你想做的事情,但这样你就可以跟踪输入,祝你好运

s,d,p = [],[],[]
for x in range(3):
        s.append(input("What is item name: "))
        d.append(int(input("How many of these items are in stock? ")))
        p.append(float(input("How much is each unit price? ")))
for i in range(3):
        print("Description:", s[i])
        print("Units:", d[i])
        print("Price:", p[i])

另一种可能是创建一个数组:

array=[]、[]、[]
对于范围(3)内的x:
数组[0]。追加(输入(“项目名称:”))
数组[1]。追加(int(输入(“这些项目中有多少有库存?”))
数组[2]。追加(float(输入(“每个单价是多少?”))
对于范围(3)中的i:
打印(“说明:”,数组[0][i])
打印(“单位:”,数组[1][i])
打印(“价格:,数组[2][i])

在循环中,您输入的每一项(最后一项除外)都会被替换,因为您每次都存储到同一个变量。这看起来很奇怪:
对于范围(3)中的项目: