Python While循环不显示正确的输出

Python While循环不显示正确的输出,python,dataframe,while-loop,do-while,Python,Dataframe,While Loop,Do While,我创建了一个while循环来获得以下输出= 输入玩家的名字-1-Alex 输入玩家的名字-2-Peter 输入玩家的名字-3-。。。 但是,我无法正确实现while循环,因为我总是获得 相同的值=1。有人能帮我吗? 代码= ------------ 作为pd进口熊猫 将numpy作为np导入 导入seaborn作为sns 将随机导入为随机 将matplotlib.pyplot作为plt导入 进口统计 输入数学 进口统计 将numpy作为np导入 导入scipy.stats 作为pd进口熊猫 将

我创建了一个while循环来获得以下输出=

输入玩家的名字-1-Alex

输入玩家的名字-2-Peter

输入玩家的名字-3-。。。

但是,我无法正确实现while循环,因为我总是获得 相同的值=1。有人能帮我吗? 代码=

------------
作为pd进口熊猫
将numpy作为np导入
导入seaborn作为sns
将随机导入为随机
将matplotlib.pyplot作为plt导入
进口统计
输入数学
进口统计
将numpy作为np导入
导入scipy.stats
作为pd进口熊猫
将matplotlib.style导入为样式
从日期时间导入时间增量
将日期时间导入为dt
导入时间
导入操作系统
#游戏玩家的名字
i=1
科隆=['player_name']
df=[]
对于科隆的玩家:

而我一个更简单的方法可能是使用.format(),除了首先创建所有玩家的列表,然后在最后将其转换为数据帧之外。请参见下面的示例。使用.format()可以避免字符串中不同数据类型的问题

import pandas as pd

players = []  # empty list 
i = 1  # initial count and player number 

while i < 11:

    players.append(input(('Input the name of Player {}').format(i)))  # take input and append to list 

    i += 1

df = pd.DataFrame(players, columns=['Player Name'], index=[i+1 for i in range(10)])  # convert list to dataframe
将熊猫作为pd导入
玩家=[]#空名单
i=1#初始计数和玩家编号
而我<11:
append(输入(('input the name of Player{}').format(i))#获取输入并附加到列表
i+=1
df=pd.DataFrame(players,columns=['Player Name'],index=[i+1表示范围内的i(10)])#将列表转换为DataFrame

在while循环中,您使用的是一个列表理解,该列表通过范围(10)中的数字(玩家)循环

但是,您使用i作为播放器的编号。不确定您是否打算同时使用while循环和list-comprehension循环,但如果您同时使用while循环和list-comprehension循环,则只需更改:

input("Enter the name of the Player %s " %i)
致:

否则,所有10个版本的循环都将发生在第一个while循环中,因此前10个循环的i仍然是1,因为您还没有达到i=i+1

这将要求输入100行,但你可能不是这个意思,所以你可以删除列表理解,即

df_1 = pd.DataFrame({'Numero': [1,2,3,4,5,6,7,8,9,10],
    player: [input(f"Enter the name of the Player %s " %i)],
    }).set_index('Numero')

你的意思是
%player
,而不是
%i
。。。对于范围(10)内的玩家,
循环运行十次,而
i
仍然为1。正如@Carcigenicate所评论的,您是否打算使用
player
作为值,而不是
i
?@JohnGordon是的,它是有效的!谢谢你们俩
input("Enter the name of the Player %s " %player)
df_1 = pd.DataFrame({'Numero': [1,2,3,4,5,6,7,8,9,10],
    player: [input(f"Enter the name of the Player %s " %i)],
    }).set_index('Numero')