【问题标题】:While loop functionwhile循环函数
【发布时间】:2021-01-14 22:29:48
【问题描述】:

我想设置一个 while 循环函数或任何其他可能性,以便使用一次 str(input()) 而不要重复操作 10 次。

# Name of the player for the game
colonne= ['player_name']
df = []
for player in colonne:
    df_1 = pd.DataFrame(
        {
            'Numéro': [1,2,3,4,5,6,7,8,9,10],
             player: [
                 str(input()),
                 str(input()),
                 str(input()),
                 str(input()),
                 str(input()),
                 str(input()),
                 str(input()),
                 str(input()),
                 str(input()),
                 str(input()),
            ],
        }
    ).set_index('Numéro')
    df.append(df_1)
df = pd.concat(df, axis=1)
print(df)

【问题讨论】:

  • 将列表替换为[str(input()) for _ in range(10)]
  • 您也可以使用list(range(1,11))创建“Numero”列表

标签: python loops while-loop


【解决方案1】:

用循环替换输入:

# Name of the player for the game
colonne= ['player_name']
df = []
for player in colonne:
    df_1 = pd.DataFrame(
        {
            'Numéro': [1,2,3,4,5,6,7,8,9,10],
             player: [str(input()) for _ in range(10)],
        }
    ).set_index('Numéro')
    df.append(df_1)
df = pd.concat(df, axis=1)
print(df)

我也建议重命名Numéro,使用变音符号可能会导致将来在不同系统中出现问题。

【讨论】:

    【解决方案2】:

    您可以将列表替换为,

    for i in range(0,10):
        player.append(str(input()))
    

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 2023-03-16
      • 2017-06-07
      • 2021-03-08
      • 2016-07-23
      • 2016-11-27
      相关资源
      最近更新 更多