【问题标题】:How to ask user for number of items and then assign each item a number?如何向用户询问项目数量,然后为每个项目分配一个数字?
【发布时间】:2018-12-07 14:06:29
【问题描述】:

我在网上找不到任何东西,所以我想知道是否有人可以提供帮助。我有以下代码:

x = str(input("Enter number of unique customers then press enter: "))

我希望它向用户询问用户输入的客户数量的具体数字。例如说用户输入“2”。我想问:客户1的号码?客户 2 的号码?

我找不到查看变量 x 并使用答案来知道要问多少的方法。谢谢!

【问题讨论】:

    标签: python python-3.x dictionary syntax user-input


    【解决方案1】:

    我认为你需要循环:

    num = int(input('How many customers there are?')
    list_customer = []
    for i in range(num):
        a = input('number for customer x')
        list_customer.append(a)
    
    print(list_customer[0]) # Number for customer 1
    print(list_customer[1]) # Number for customer 2
    

    【讨论】:

    • 当然以后你会写成一个列表理解:customers = [input(f'number for customer {i + 1}') for i in range(num)].
    【解决方案2】:

    我认为这就是你想要做的。它需要输入的客户数量。制作一个列表,然后遍历该列表以获取该客户编号的输入。并将其转储到results 数据帧中

    import pandas as pd
    
    results = pd.DataFrame()
    
    x = str(input("Enter number of unique customers then press enter: "))
    
    list_of_cust = list(range(0, int(x)))
    
    for i in list_of_cust:
        x2 = str(input("Enter number for customer %d: " %(i+1)))
    
        temp_df = pd.DataFrame([[str(i+1), x2]], columns=['cust_no','value'])
        results = results.append(temp_df).reset_index(drop = True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      相关资源
      最近更新 更多