请问各位大佬,如何在python中实现这个功能? 先输入要买的水果种类总的数量 然后根据输入的总的数

请问各位大佬,如何在python中实现这个功能? 先输入要买的水果种类总的数量 然后根据输入的总的数量,提供相应数量的输入选项,可以分别输入水果的名称和重量,然后再用一个列表把这些数据装在一起。 例如 输入要买水果的种类是4种 然后分别输入 苹果,2公斤 ;香蕉,3公斤;梨1公斤;菠萝,3公斤。再把以上的数据放入一个列表。 新人不胜感激!

 

count = input("Please enter number of products: ")

fruit = []

for i in range(4):
    name = input(f"Name of product {i+1}: ")
    weight = input(f"Weight of product {i+1}: ")
    fruit.append( (name, weight) )

print(fruit)

运行结果:

>>> count = input("Please enter number of products: ")
Please enter number of products: 4
>>> fruit = []
>>> for i in range(4):
...     name = input(f"Name of product {i+1}: ")
...     weight = input(f"Weight of product {i+1}: ")
...     fruit.append( (name, weight) )
...
Name of product 1: Apple
Weight of product 1: 2
Name of product 2: Banana
Weight of product 2: 3
Name of product 3: Pear
Weight of product 3: 1
Name of product 4: Pineapple
Weight of product 4: 3
>>> print(fruit)
[('Apple', '2'), ('Banana', '3'), ('Pear', '1'), ('Pineapple', '3')]
>>>

如果你满意我的回答,请点采纳,我很感谢你的认可。