0

So im new to coding so please dont judge my coding. I have made this function that reads a csv file and finds info about a fotball match if the user input matches the info in the file. First it checks if the number(runde) matches the numbers in the file. And then a coressponding name to a name in the csv file. then it will print info if it has found a match. My problem is that all of it works one time, then when the while loop starts a second time it wont work and says it cant find a match. Not sure where the problem is.

import csv

filnavn = "kamper_toppserien.csv"

with open (filnavn, encoding="utf-8-sig") as fil:
    filinnhold = csv.reader(fil, delimiter=";")
    overskrifter = next(filinnhold)
    
    #lager funskjon
    def kampInfo(runde, lagnavn):
        
            funnet = 0
             
            #går igjennom radene
            for rad in filinnhold:
                
                #lager variabler som skal brukes
                erunde = rad[0]
                
                ehjemmelag = rad[4].split()
                hjemmelag = rad[4]
                
                bortelag=rad[5]
                ebortelag = rad[5].split()
                
                resultat = rad[6]
                bane = rad[7]
                klokke= rad[3]
                dato = rad[1]
                
                #finner brukerens runde i tabellen
                if runde == int(erunde):
                   #sjekker om lagnavnet finnes for den runden
                   if lagnavn.split()[0] in ehjemmelag or lagnavn.split()[0] in ebortelag:
                       print(f"Kamp: {hjemmelag} mot {bortelag}. ")
                       print(f"Resultatet av kampen ble {resultat} ")
                       print(f"Kampen ble spilt på {bane} klokken: {klokke}, dato: {dato} ")
                       funnet+=1
                       
            if funnet ==0 :
                    print("Fant ikke")
    ferdig = False           
    while ferdig == False  :
               
        try:                
            print("Tast 0 i runde for å avslutte søket")            
            runde = int(input("runde: "))
             
            if runde == 0:
                ferdig = True
            else:
                
                print("Skriv lagnavv med stor forbokstav")
                lagnavn = str(input("Lagnavn: ")) 
                
                if lagnavn.isdigit():
                    print("Sjekk at du har skrevet lagnavn og ikke tall.")
                else:
                    kampInfo(runde, lagnavn)
        except:
            print("Har du husket å skrive runden som et tall(int)? ")
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hello, and welcome to StackOverflow! Would it be possible for you to edit your question so your code is in English. since that will make it much easier for us to understand what's up with it. – Miguel Guthridge Apr 24 '23 at 16:37
  • `filinnhold` is read until the end by the for-loop. If it is used again in the for-loop, the loop is not even entered because there is nothing more to read. – Michael Butscher Apr 24 '23 at 16:40
  • Thank you so much for the help! Sorry for not translating it to English, and for writing the question so messy. Was desperate and really confused about how the formatting when asking a question here works... – aleks_mj Apr 24 '23 at 21:56

0 Answers0