1
comp_sc = 0
game_start = False

def main():
    turn_start = input('Are you Ready to play? ').lower()
    if turn_start == 'n':
        game_start = False
        print('No one is willing to play!!!')
    if turn_start == 'y':
        game_start = True

    #while game_start == True:
    for x in range(1, 5):
        com_move(comp_sc)

def roll():
    ***


def com_move(comp_sc):
    turn_score = int(roll())
    if turn_score < 6:
        comp_sc =+ turn_score
        print(comp_sc, 'comp_sc')
    elif turn_score == 6:
        comp_sc =+ 0
        game_start = False
    return comp_sc

in my com_move function, I don't see turn_score (which is outputting a random number through random module) adding it in comp_sc variable. when I run this function - comp_sc is always equal to turn_score - instead of adding up all the 5 turn_score in it.

Thank you

2
  • 1
    Use the global keyword to write to global variables. Commented Sep 11, 2021 at 18:02
  • 1
    Use += and not =+ to add to computer_score Commented Sep 11, 2021 at 18:05

2 Answers 2

2

computer_score is a local variable inside the computer_move function. You are doing the correct thing by returning it, but then you just ignore this return value. Instead, you could assign it back to the computer_score variable in the calling function:

for x in range(1, 5):
    computer_score = computer_move(computer_score, human_score)
Sign up to request clarification or add additional context in comments.

Comments

1

It is because you write computerscore =+ turnscore which is setting computerscore to the positive value of turnscore, so they will always be the same.

The right way would be to write computerscore += turnscore which will add the turnscore to the computerscore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.