-1

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```
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Where *exactly* are you encountering the error? It’s really not up to us to try and work out where the issue is. It’s really your job to tell us, and help us help you. – S3DEV Aug 03 '22 at 17:18
  • 1
    You set `opsum` and `yoursum` to zero at the beginning of each iteration. Do that outside of the loop. – JNevill Aug 03 '22 at 17:18
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 03 '22 at 17:19
  • Try making a small test program focusing on just incrementing a variable in a loop. That makes a better thing to post here than a bunch of code. Often, writing the test script answers your question. – tdelaney Aug 03 '22 at 17:20
  • Sorry about the annoying format and such. only my second question. Basically, I run into the issue when a team "makes a basket" in my code, the score is printed 0-0 anyway. Even though the made basket is supposed to be added to the score. – Jacob Ferbrache Aug 03 '22 at 17:22

1 Answers1

1

As noted in my comment, you set oppsum and yoursum to 0 inside the loop. So every iteration you reset those values back to 0. Instead set them to 0 before the loop starts:

from time import sleep
print("You've got the ball!")
yoursum = 0
oppsum = 0
while True:
    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

To illustrate, imagine a simple example:

for i in range(10): 
    myscore = 0 
    myscore += 1
    print(myscore)

That prints out 0 ten times as you can imagine. Each iteration sets myscore back to 0 and then adds 1 to it.

Instead:

myscore = 0 
for i in range(10):        
    myscore += 1
    print(myscore)

Now myscore is set to 0 and the loop begins adding 1 each iteration and printing 1,2,3,4,5,6,7,8,9,10

Regarding the note I made in the in the comments about your score variable not being updated before you run your various print(score) consider a function to update and print the score that you can call during your different states:

import random
from time import sleep
yoursum = 0
oppsum = 0
print("You've got the ball!")
while True:   
    def print_score():
        score = (f"The score is: {yoursum}, {oppsum}.")
        print(score)

    if yoursum >= 4:
        print("You won! Great Game.")
        break
    elif oppsum >= 4:
        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
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • I made this change, unfortunately as I play through, the score continues to return as 0-0 each time. I feel like it may be an issue with my lines "if randopp == "Makes a two" – Jacob Ferbrache Aug 03 '22 at 17:27
  • It works for me. Or rather, it doesn't work correctly, but the values increase with certain actions. Those actions don't match up to my expectations of how a score in basketball should increase because the code is still buggy, but the scores do increase. One of the things that sticks out to me is that `score = (f"The score is: {yoursum}, {oppsum}.")` happens at the top of the iteration but `print(score)` is littered throughout the code after the score changes. You need to do `score = (f"The score is: {yoursum}, {oppsum}.")` every time the score changes to update the score string. – JNevill Aug 03 '22 at 17:37
  • There's other bugs here and there, but it's a little broad for this particular stackoverflow question to troubleshoot and add to the answer. – JNevill Aug 03 '22 at 17:38
  • I lied a bit when I said I wouldn't troubleshoot further. I updated the question with a suggestion on your `score` updating issue and `print(score)` to solve with a function that takes care of both updating and printing. I think everything works as expected with that change. Whatever other bugs may pop up in your testing are probably easily addressable. – JNevill Aug 03 '22 at 17:45