I have made an "interactive" basketball game as a beginner project. I can't figure out how to make the game tally up the score as the loop continues. Here is my Code, please try it out and see if you can get the code to keep score. I am trying to get the game to go to 21 before the while loop breaks. I have oppsum and yoursum starting at 0 and according to actions are supposed to go up by 2 or 3 points.
The error: As mentioned in the comments, moving the sums to outside of the loop was step 1. Then, I needed to get rid of the "Score" variable inside the loop at the top, and instead moved that corresponding f string into the places to be printed instead of the "score" variable.
from time import sleep
print("You've got the ball!")
while True:
yoursum = 0
oppsum = 0
score = (f"The score is: {yoursum}, {oppsum}.")
if yoursum >= 21:
print("You won! Great Game.")
break
elif oppsum >= 21:
print("You lost. Better luck next time.")
break
off = input("Press 1 to pass, 2 to shoot, or 3 to drive to the hoop!\n")
passing = ["two", "three", "steal"]
shot = ["it's good", "no good"]
opp = ["makes a two.", "makes a three.", "misses the shot. Rebound defense.", "gets it stolen!"]
randopp = random.choice(opp)
randshot = random.choice(shot)
randpass = random.choice(passing)
twofeet = random.randint(2,20)
driver = random.randint(2,7)
closeshot = random.randint(1,100)
farshot= random.randint(1,100)
medshot= random.randint(1,100)
threefeet = ["the corner", "the wing", "the top of the key"]
randthree = random.choice(threefeet)
if off == "1":
if randpass == "two":
sleep(.5)
print(f"Teammate catches it at {twofeet} feet")
elif randpass == "three":
sleep(.5)
print(f"Teammate catches it at {randthree}")
elif randpass == "steal":
sleep(.5)
print("Defense steals the pass!")
sleep(0.75)
print("Opponent has the ball")
sleep(0.75)
print(f"Opponent {randopp}")
if randopp == "makes a two.":
oppsum += 2
if randopp == "makes a three.":
oppsum += 3
print(score)
continue
if off == "2":
sleep(.5)
print(f"Shot goes up, {randshot}!")
if randpass == "three" and randshot == "it's good":
yoursum += 3
if randpass == "two" and randshot == "it's good":
yoursum += 2
else:
yoursum += 0
print(score)
sleep(0.75)
print("Opponent has the ball")
sleep(0.75)
print(f"Opponent {randopp}")
if randopp == "makes a two.":
oppsum += 2
if randopp == "makes a three.":
oppsum += 3
sleep(.4)
print(score)
continue
if off == "3":
sleep(.5)
print(f"Drives in to {driver} feet!")
continue```