-3

我通过搜索来打印 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("**********************************************************************")
        
 
4

1 回答 1

0

找到匹配项时设置一个变量。在循环结束时检查变量,以便您可以说未找到匹配项。

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("**********************************************************************")
于 2021-08-22T17:04:00.003 回答