【问题标题】:how can you ask for n input from a user and then ask for more inputs based on those n inputs in python?你怎么能要求用户提供 n 个输入,然后根据 python 中的 n 个输入要求更多输入?
【发布时间】:2023-02-25 12:01:22
【问题描述】:

例如:您销售多少产品? input = 2, 曲奇牛奶

饼干: 输入:他们花了多少钱: 输入: 销售价格:

牛奶: 输入:他们花了多少钱: 输入: 销售价格:

prod_num - (str(input("你卖了多少产品:"))) products = list(map(str, input("请列出您销售的产品(例如:面包饼干牛奶)").split()))

【问题讨论】:

  • 询问有多少产品,将该输入转换为整数。然后循环多次以询问每种产品。
  • @JohnGordon 我想到了这一点,但是您如何做到例如:每个产品的销售价格不会在循环中相互覆盖?

标签: python


【解决方案1】:

所以你可以做的一件事是,在询问这个人销售的产品是什么之后,你遍历每一个产品,并询问这个人产品的价格,如下所示:

productsCount = input("How many products do you sell?");

products = input("Please list the products you sell, separated by a comma: ").split(",");

prices = [];

for i in range(len(products)):
    prices.append(input("How much does the " + products[i] + " cost?"));

print(prices);

通过这种方式,您可以按正确的顺序获得包含该人销售的产品的列表,以及包含每种产品价格的另一个列表。

老实说,我不太明白你想要什么,我想就是这样。如果您需要其他任何东西,或者如果我的回答不是您想要的,请告诉我!

【讨论】:

    【解决方案2】:

    要求用户输入数字:

    product_amount = int(input("How many products?: "))

    然后,存储用户响应。我会这样做的方式是在嵌套列表中:

    product_info = []
    
    for x in range(product_amount):
        current_product = []
        current_product.append(input("Product name: "))
        # repeat for as many questions as necessary
        product_info.append(current_product)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多