-3

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("**********************************************************************")
        
 
  • But, if you enter something which *is* found, I presume that you get some output. But, what happens next? – quamrana Aug 22 '21 at 16:57
  • Your script only prints something when it finds a match. – Barmar Aug 22 '21 at 17:00
  • If you want people to debug your code, provide a [mcve]. What's `Markdown`? `Console`? This is just an out-of-context function. Also, never post images of text. People can't create a `location2.csv` without manually typing your data. – Mark Tolonen Aug 22 '21 at 17:05
  • but I'm trying to say that if it doesn't find a match, it should pop up with an error message and run the function again but I can't figure out how to do it. – Olivia Smith Aug 22 '21 at 17:09

1 Answers1

0

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("**********************************************************************")
Barmar
  • 741,623
  • 53
  • 500
  • 612