请问各位大佬,如何在python中实现这个功能?
先输入要买的水果种类总的数量
然后根据输入的总的数量,提供相应数量的输入选项,可以分别输入水果的名称和重量,然后再用一个列表把这些数据装在一起。
例如 输入要买水果的种类是4种
然后分别输入 苹果,2公斤 ;香蕉,3公斤;梨1公斤;菠萝,3公斤。再把以上的数据放入一个列表。
新人不胜感激!
1条回答 默认 最新
- benbenli 2021-05-10 12:40关注
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')] >>>
如果你满意我的回答,请点采纳,我很感谢你的认可。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 基于卷积神经网络的声纹识别
- ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
- ¥100 为什么这个恒流源电路不能恒流?
- ¥15 有偿求跨组件数据流路径图
- ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
- ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
- ¥15 CSAPPattacklab
- ¥15 一直显示正在等待HID—ISP
- ¥15 Python turtle 画图
- ¥15 stm32开发clion时遇到的编译问题