所以当我输入 csv 文件中没有的内容时,它就会停止 运行

so when I enter something thats not in the csvfile, it will just stop running

我正在通过搜索打印 CSV 文件中的内容,但如果我输入 CSV 文件中没有的其他名称,它会出于某种原因停止 运行。例如,当我输入不在 CSV 文件中的 (hello) 时,它会像结束程序一样停止。我需要帮助来修复我尝试过的 else 语句,但它没有用。我在下面提供了 CSV 文件的图片。

def booking_court()

    location = (console.input("[bold bright_yellow]Please Enter the Name of the Chosen Gym:[/] ").lower().title())
    with open("location2.csv") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=',')
        for idx, row in enumerate(csvreader):
            if idx == 0:
                titles = row
            elif location in row[0]:
                for key, value in zip(titles, row):
                    console.print("[bold yellow]%s: [/][bold blue]%s[/]" % (key, value))
            
                
                console.print("**********************************************************************")
        
 

找到匹配项时设置一个变量。在循环结束时检查变量,这样你就可以说没有找到匹配项。

def booking_court()
    console = Console()
    console.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", style="bold blue")

    MARKDOWN = """
        Find the Closest Gym Near You!

            1 . Challenge Volleyball 
            2 . CitySide Sport  
            3 . Kew Sport 
            4 . Melbourne University Renegades Volleyball Club
            5 . Rebound Indoor Beach Volleyball   
            6 . Sand Volleyball Court  
            7 . Volleyball Courts
            8 . Volleyball Halls
            9 . Volleyball Victoria Inc 
            10 . Westside Indoor Sports 

         These are the available facilities in the State of
                            Victoria 
----------------------------------------------------------------------
            Please Select a Gym that is Nearest to You!               
        Note*: Please Enter the Name of Chosen Location: 
        ***Note Distance Provided is from Victoria's CBD***                 

                                                                         """
    console = Console(width=70)
    md = Markdown(MARKDOWN)
    console.print(md, style="bold blue")
    console.print(" ****Note Distance Provided is from Victoria's CBD****", style="bold red")

    found = False
    location = (console.input("[bold bright_yellow]Please Enter the Name of the Chosen Gym:[/] ").lower().title())
    with open("location2.csv") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=',')
        for idx, row in enumerate(csvreader):
            if idx == 0:
                titles = row
            elif location in row[0]:
                for key, value in zip(titles, row):
                    console.print("[bold yellow]%s: [/][bold blue]%s[/]" % (key, value))
                found = True
    
    if not found:
        console.print("[bold red]No matching locations found[/]")
                
                console.print("**********************************************************************")