要求用户继续查看接下来的 5 行数据



我正在尝试获取接下来的 5 个数据行,而无需每次都编写循环([:5]、[5:10]、[10:15]等(。我有一些想法,但我被困在这个上面一段时间了,有什么方法可以让用户显示下一个 5 个?

def raw_data(df):
ask_user = input('Would you like to view more raw data for the city selected? nPrint yes or no: ')
while True:
if ask_user == 'yes':
return df.iloc[:5]
else:
break

添加一个每次函数运行时累积的变量。(我们称之为runs(在函数外部初始化它,并在函数内部添加一个。从这里,将其乘以 5 得到您的范围。

runs = 0
def raw_data(df):
while True: 
ask_user = input('Would you like to view more raw data for the city selected? nPrint yes or no: ') 
if ask_user == 'yes': 
runs += 1 #Adds 1 to current value, same as runs = runs + 1
return df.iloc[(x-1)*5:x*5]
elif ask_user == 'no':
return

最新更新