Search code examples
pythonincrement

Why is my code not incrementing the count?


I have a function (from a part of my code) which is supposed to increment the count whenever I run the function, however, the count only stays at 1. What am I doing wrong? Thanks in advance.

def add_a_team():
    csv_team_count = 0
    team_adder = []
    csv_team_count += 1
    print(csv_team_count)

    if csv_team_count <4:
        for i in range(0,6):
            name = str(input("You are now entering the data for Team 1.\nYou will see this 6 times, enter the team's name first then the members, all individually."))
            team_adder.append(name)

    else:
        print("Team count of 4 has been filled in! You cannot add another team!")

    print("\n")

    with open('teams.csv', 'a', newline = '', encoding='UTF8') as f:
        writer = csv.writer(f)
        #writing the data for each team.
        writer.writerow(team_adder)

    f.close()

Solution

  • Everytime you initialize the function the value is set to 0.

     csv_team_count = 0
    

    After you increment to 1, but if you call again the function it will set to 0. This value needs to be set outside the function and be passed as arg.