【问题标题】:Python unable to run programPython无法运行程序
【发布时间】:2021-12-19 13:39:50
【问题描述】:

所以我已经在这个程序上工作了一段时间,由于某种原因,Spyder (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循环是如何工作的!在您的情况下,while option != 0: while 循环将进入无限循环序列,程序将无法运行。

标签: python pandas dataframe csv pd


【解决方案1】:

我认为您的代码可能存在多个问题,但首先我会检查以下内容:它可能会陷入无限循环:)

一开始您的代码如下所示:

while option != 0:
    if option == "A":
        print = "providing you with local businesses now, please input criteria:"  

只有当你的输入正好是0,它才会通过这个while循环。如果不是,它将进入while循环并在那里执行一个无限循环。 print 方法在那里也不起作用,因为您尝试执行:

print = "some text..."

这不起作用(实际上用你的字符串替换了内置的 print 方法),你应该用 `print("some text...") 替换这些行。

如果您的 .csv 有问题,它也可能会在那里崩溃,但我无法检查,如果是这种情况,我认为您会收到错误消息。

代码可能有更多问题(我看到更多无限循环的可能性,例如while x in df),但我会从这些开始! :)

【讨论】:

  • 您好!我之前确实意识到问题出在无限循环上,但我似乎无法打破它,exit() 不起作用, sys.exit() 不起作用,我尝试的所有其他方法都不起作用。也许问题在于我在哪里编写代码? (我在这里可能没有多大意义,但我是新手,所以我是一名高中生!)无论如何,再次提供任何帮助将不胜感激! (就“while x in df”语句而言,我确实将它们隔离并运行并且它们确实有效。所以我猜我的问题是无限循环)
  • 好吧!在这种情况下,调试代码时总是对我有帮助的是使用“交互式调试器”。您可以通过多次输入import pdb; pdb.set_trace()' at the point in your code where you want to enter the debugger. Then you can press n` 行来做到这一点,您将看到哪些代码行以何种顺序执行。希望你会看到你的代码卡在哪里。祝你好运! (有关调试器的更多信息:docs.python.org/3/library/pdb.html
  • 如果exit() 不起作用,这意味着你的无限循环在我认为的代码中的某个位置!它也可能有助于放下一些打印语句,以查看您的代码到达了哪些行。
  • 我不知道那个调试器,所以非常感谢!我肯定会把它留到以后!至于现在,我已经通过调试器运行了代码并确认了怀疑,问题出在菜单之后的第一个 while 循环中(第 22/23 行),但是当我尝试发出 break() 或 exit()紧随其后的声明,它说它在循环之外!这听起来一定很简单,但如果正确的话,我不知道如何将语句放入循环中?
  • 好的,所以更新:我确实尝试在故障循环之前添加一条语句以满足导致无限循环的非零条件,代码只是运行通过它。所以我想要做的是:“while option != 0: continue else:break”但很明显该语法是无效的,我想在之后继续使用这些选项。你知道如何执行吗?
猜你喜欢
  • 2023-02-04
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 2020-10-21
  • 2022-09-28
相关资源
最近更新 更多