为什么我的all_data函数一直循环?

why does my all_data function keep looping?

提问人:ain 提问时间:6/7/2023 更新时间:6/7/2023 访问量:28

问:

import pandas as pd

df = pd.read_csv('sales_data.csv')

def main_menu():
    print("Main Menu")
    print("1) Return all current sales data")
    print("2) ")
    print("3) ")
    print("4) ")
    print("5) ")
    print("x) Exit")
    return input("Enter your choice: ")

def all_data():
    return df

# main menu loop
x = main_menu()
while x in ["1", "2", "3", "4", "5", "x"]:

  if x == "1":
    all_sales = all_data()
    print(all_sales)

当我输入“1”时,all_data函数会不停地循环打印我的数据帧。我该如何解决?

python pandas 函数 while-loop

评论

0赞 Vishnudev Krishnadas 6/7/2023
你这个声明背后的意图是什么?因为如果输入在列表中,那么它就是一个无限循环。while x in ["1", "2", "3", "4", "5", "x"]

答:

0赞 locchuong 6/7/2023 #1

当你给出输入“1”时,程序会进入循环并检查条件,然后它会执行代码后,循环循环不停任何停止条件,如果你要出去什么,就要更新值。例:x = main_menu()whileif x == "1":ifwhilex

import pandas as pd

df = pd.read_csv('sales_data.csv')

def main_menu():
    print("Main Menu")
    print("1) Return all current sales data")
    print("2) ")
    print("3) ")
    print("4) ")
    print("5) ")
    print("x) Exit")
    return input("Enter your choice: ")

def all_data():
    return df

# main menu loop
x = main_menu()
while x in ["1", "2", "3", "4", "5", "x"]:

  if x == "1":
    all_sales = all_data()
    print(all_sales)
    x = main_menu()