Anaconda Spyder运行Python商业分析程序无报错失败求助
商业分析Python程序运行无报错直接故障问题排查
我开发了一款商业分析Python程序,在Anaconda的Spyder环境中运行时无任何错误提示但直接失败,推测可能是循环未闭合类问题但无法定位具体原因。
完整程序代码
import pandas as pd df = pd.read_csv(r"C:\Users\user\Desktop\Small_businesses1.csv",encoding='latin1') #create start menu def menu(): print("Hello!, Welcome to business analysis, how may we help you?") print("A) Help you find a local business based on your criteria?") print('B) Provide you with performance analytics of businesses and industries around you?') print('C) To access and read an excerpt from our data file:') print('D) Provide you an exit from the interface') menu() option = str(input("Kindly enter your option here: ")) while option != 0: if option == "A": print="providing you with local businesses now, please input criteria:" df=pd.read_csv('small businesses.csv') w=str(input('enter name here')) df = pd.DataFrame({"name": ['Starz Café','V lounge salon','Swapnil Tyres','Game Dome','Pride Super Shopee','Lifeberries healthcare' ,'365 medical','Mad momoz','Monde Collections','Studio Hub IT','Healer Armis','Tumbledry Laundry','VCF healthclub','Max link Sports','Creamstone' ,"Archie's","Milano's café",'Café old skool','Rebellion Apprel','D.C books','Agrwal Sweets','grocers','Ronak super mart','Vision stationary']}) while (w in df): print(w) if w=='': break if w!=0: print("Please enter a valid input from the given list") x=str(input('enter field here')) df = pd.DataFrame({"field": ['Food & Hospitality','Beauty, health, fitness','Automotive Repair','Sports & Recreation','General Retail','Medical Services','Medical Services (pharmacy)','Food & Hospitality','General Retail','Technology','Medical services','Cleaning and Maintance','Health, beauty and Fitness','Health, beauty and Fitness','Food & Hospitality','General Retail','Food & Hospitality','Food & Hospitality','General Retail','Stationary & Education','Food & Hospitality','General Retail','General Retail','Stationary & Education']}) while (x in df): print(x) if x=='': break if x!=0: print("Please enter a valid input from the given list") y=str(input('enter desired operation size here')) df = pd.DataFrame({"operation size": ['Very small','Moderate-Large' ,'Moderate' ,'Moderate' ,'Small','Moderate' ,'Small','Moderate' ,'Small','Small','Small','Large','Moderate' ,'Moderate' ,'Moderate' ,'Large','Small','Small','Small','Small','Small','Medium','Small','Small']}) while (y in df): print(y) if y=='': break if y!=0: print("Please enter a valid input from the given list") z=str(input('enter preferred mode of service here')) df = pd.DataFrame({"mode of service": ['Physical dine-in/Online delivery','Physical service','Physical service, Home service','Physcial service','Physical service, Home service','Physical service, Online diagnosis','Physical service','Online delivery','Physical service/remote sevice','Remote/online service','Physical service/diagnosis','Physical service/remote sevice','Physical','Physical','Physical service','Physcial service','Physcial service, Online delivery','Physical service, Online Delivery','Online delivery','Physical service, Home service','Physical service, Home service','Physical service, Home service','Physical service, Home service','Physical service, Home service']}) while (z in df): print(z) if z=='': break if z!=0: print("Please enter a valid input from the given list") if w == '': print (x + y + z) if x == "": print(w + y + z) elif y == "": print(w + x + z) elif z == "": print(w + x + y) elif x == "" and y == "": print(w + z) elif x == "" and z == "": print(w + y) elif y == "" and z == "": print(w + x) elif w == "" and x == "": print(y + z) elif w == "" and z == "": print(x + y) elif w == "" and y == "": print(x + z) if option == "B": #do option B stuff here if option == "C" : import csv f=open('smallbusinesses.csv', 'rt') myReader = csv.reader(f) for row in myReader: print(row) if option == "D": print("Thank you for using our interface!") menu() option = str(input('kindle enter your option here:')) print="providing you with local businesses now, please input criteria:"
关联CSV文件截图

已定位的问题点
- 缩进严重错误:
while option != 0:下方所有代码都没有缩进,导致循环体只有if option == "A":一行,后续逻辑完全脱离循环,程序执行完这行判断后直接跑后续的输入逻辑,不符合预期的菜单交互流程 - 内置函数被覆盖:多处出现
print="providing you with local businesses now, please input criteria:"这类写法,直接把Python内置的打印函数print赋值成了字符串,后续所有print()调用都会失效,这也是看不到任何报错的核心原因之一 - 类型匹配错误:
option是通过str(input())拿到的字符串类型,循环终止条件写的是while option != 0,0是整型,二者永远不相等,循环会无限执行,也不可能正常触发退出逻辑 - DataFrame取值判断逻辑错误:
while (w in df)这类写法,实际是判断输入值是否在DataFrame的列名列表中,而不是判断输入值是否在对应列的取值里,完全不符合校验输入是否在给定商家/行业列表中的业务需求 - 条件嵌套逻辑完全错误:
option == "B"的代码块里嵌套了option == "C"、option == "D"的判断,同一时间option只能取一个值,选中B之后永远不可能触发C、D的逻辑 - 文件路径不统一:代码中先后三次读取csv文件,路径分别是
C:\Users\user\Desktop\Small_businesses1.csv、small businesses.csv、smallbusinesses.csv,路径完全不统一,大概率会触发文件找不到的报错,只是因为print函数被覆盖看不到报错信息 - 输入校验逻辑错误:
if w!=0这类判断中,w是字符串类型,和整型0比较永远返回True,不管用户输入什么内容都会提示“请输入给定列表中的有效值”
内容的提问来源于stack exchange,提问作者Shash
相关产品推荐
相关产品推荐

