0

I just started learning to code.

I'm trying to write this simple counter. It works on the first run, however when the loop calls "while()" it resets both "r" and the lists "we_list" "you_list". I can't figure how to store their value even after the loop.

def begin():
    r = 1
    print("This is a counter for the game Belote")
    print("Round " + str(r))

    we_list = []

    you_list = []

    we = int(input("Enter score for 'We' "))
    we_list.append(we)
    we_sum = sum(we_list)

    you = int(input("Enter score for 'you' "))
    you_list.append(you)
    you_sum = sum(you_list)

    print("WE " + str(we_sum))
    print("YOU " + str(you_sum))
    r += 1
    while we_sum or you_sum < 151:
        begin()
    else:
        print("End of game ")
        exit()
begin()

Edit:

I edited the code with the suggestions, and managed to fix r and and the lists, however now the problem that I have is that it does not break out of the loop after 151.

we_list = []
you_list = []

def begin(r):
    print("This is a counter for the game Belote")
    print("Round " + str(r))

    we = int(input("Enter score for 'We' "))
    we_list.append(we)
    we_sum = sum(we_list)

    you = int(input("Enter score for 'you' "))
    you_list.append(you)
    you_sum = sum(you_list)

    print("WE " + str(we_sum))
    print("YOU " + str(you_sum))
    r += 1
    while we_sum or you_sum < 151:
        begin(r)
    else:
        print("End of game ")
        exit()
r=1
begin(r)
Kaloian Kozlev
  • 43
  • 1
  • 11

4 Answers4

0

you are initializing r,we_list and you_list inside begin function, so when begin is called from while each time they are initialized to r=1, you_list=[] and we_list = []. Initialize them outside begin function.

gmatharu
  • 83
  • 1
  • 8
0

Your design is a bit messy, you should isolate the "round" logic into a dedicated function, and return these values.

Also if you do not need to keep track on each value added, you do not need to keep the list, you can simply directly sum it.

def round(we, you):
    we_in = int(input("Enter score for 'We' "))
    we = we + we_in

    you_in = int(input("Enter score for 'you' "))
    you = you + you_in
    print("WE " + str(we))
    print("YOU " + str(you))

    return [we, you]

def begin():
    r = 1
    print("This is a counter for the game Belote")

    we_sum = 0  
    you_sum = 0  

    while we_sum or you_sum < 151:
        print("Round " + str(r))    
        r += 1
        [we_sum, you_sum] = round(we_sum, you_sum)
    else:
        print("End of game ")
        exit
Luc
  • 1,393
  • 1
  • 6
  • 14
0

r is a local variable, so each time begin() calls itself, that new begin() gets a new r.

You can make r, we_list and you_list globals (declare them outside of begin() or with the global keyword) and it will save the values.

BurnNote
  • 405
  • 1
  • 4
  • 13
  • When I declare them outside the function this is what I get : UnboundLocalError: local variable 'r' referenced before assignment – Kaloian Kozlev May 08 '18 at 10:19
  • Try adding `global r` on the first line of begin(), that way it it knows it should use the `r` defined outside. – BurnNote May 08 '18 at 10:57
0

Fix your code sending r as a argument

def begin(r):
    print("This is a counter for the game Belote")
    print("Round " + str(r))

    we_list = []

    you_list = []

    we = int(input("Enter score for 'We' "))
    we_list.append(we)
    we_sum = sum(we_list)

    you = int(input("Enter score for 'you' "))
    you_list.append(you)
    you_sum = sum(you_list)

    print("WE " + str(we_sum))
    print("YOU " + str(you_sum))
    r += 1
    while we_sum or you_sum < 151:
        begin(r)
    else:
        print("End of game ")
        exit()
r=1
begin(r)
Willian Vieira
  • 646
  • 3
  • 9