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)
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