Search code examples
pythoncsvcsvreader

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


I'm print what's on a CSV file by searching it up but if I enter a different name that's not on the CSV file it will just stop running for some reason. For example in the input when I enter (hello) which is not in the CSV file, it will just stop like end the program. I need help with fixing that I tried else statement but it didn't work. I have provided the pic of the CSV file below.

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("**********************************************************************")
        
 

Solution

  • Set a variable when you find a match. At the end of the loop check the variable so you can say that a match wasn't found.

    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("**********************************************************************")