Home > Software design >  Why does the score in my code is always 0?
Why does the score in my code is always 0?

Time:01-06

I've written this code and it doesn't work, it doesn't calculate the score. It needs to read the previous score from rating.txt and add to a file 50 points in case it is a draw and 100 points in case it is win. Then read the file and tell the player their score. Here is my code, help me please :

import random

person = input(str('Enter your name:'))
print('Hello,', person)

while True: 
    user_move = str(input())
    user_move = user_move.lower()

    choices= ['rock', 'paper', 'scissors']

    computer_choice = random.choice(choices)
    
    win = 0
    
    if user_move in choices:
        if user_move == computer_choice:
            win  = 50
            score = open('rating.txt', 'a')
            score.write(str(win))
            score.close()
            print(f'There is a draw ({user_move})')
        if user_move == 'rock':
            if computer_choice == 'paper':
                print(f'Sorry, but the computer chose {computer_choice}' )
            elif computer_choice == 'scissors':
                win  = 100
                score = open('rating.txt', 'a')
                score.write(str(win))
                score.close()
                print(f'Well done. The computer chose {computer_choice} and failed')
        if user_move == 'paper':
            if computer_choice == 'scissors':
                print(f'Sorry, but the computer chose {computer_choice}')
            elif computer_choice == 'rock':
                win  = 100
                score = open('rating.txt', 'a')
                score.write(str(win))
                score.close()
                print(f'Well done. The computer chose {computer_choice} and failed')
        if user_move == 'scissors':
            if computer_choice == 'rock':
                print(f'Sorry, but the computer chose {computer_choice}')
            elif computer_choice == 'paper':
                win  = 100
                score = open('rating.txt', 'a')
                score.write(str(win))
                score.close()
                print(f'Well done. The computer chose {computer_choice} and failed')
    elif user_move == '!exit':
        print('Bye!')
        break
    elif user_move == '!rating':
        string1 = person
        rating = open('rating.txt', 'r')
        readfile = rating.read()
        if string1 in readfile:
            print('Your rating:',win)
        else:
            win  =0
    else:
        print('Invalid input')

CodePudding user response:

This is because your win variable in loop is being updated to 0 every time. You need to first set it to the current score then, you need to add to it and update the file.

CodePudding user response:

You are not reading and adding to the score, you are simply appending to rating.txt the new score, that is because you are opening with 'a' and not with 'w'.

Therefore change all the a to w and change

win = 0

to

win = int(open('rating.txt', 'r').read())

And that should work.

CodePudding user response:

You can add all your score in a list and in last do sum of all values.

person = input(str('Enter your name:'))
print('Hello,', person)

win = []

while True:
    user_move = str(input())
    user_move = user_move.lower()

    choices= ['rock', 'paper', 'scissors']

    computer_choice = random.choice(choices)


    if user_move in choices:
        if user_move == computer_choice:
            win.append(50)
            print(f'There is a draw ({user_move})')
        if user_move == 'rock':
            if computer_choice == 'paper':
                print(f'Sorry, but the computer chose {computer_choice}' )
            elif computer_choice == 'scissors':
                win.append(100)
                print(f'Well done. The computer chose {computer_choice} and failed')
        if user_move == 'paper':
            if computer_choice == 'scissors':
                print(f'Sorry, but the computer chose {computer_choice}')
            elif computer_choice == 'rock':
                win.append(100)
                print(f'Well done. The computer chose {computer_choice} and failed')
        if user_move == 'scissors':
            if computer_choice == 'rock':
                print(f'Sorry, but the computer chose {computer_choice}')
            elif computer_choice == 'paper':
                win.append(100)
                print(f'Well done. The computer chose {computer_choice} and failed')
    elif user_move == '!exit':
        print('Bye!')
        break
    elif user_move == '!rating':
        print('All scores :', win)
        print('Your rating:', sum(win))
    else:
        print('Invalid input')

CodePudding user response:

Your code is good, but what you have to do is just open your file rating.txt in a write mode ('w') not append mode ('a') and also define win variable out of loop.

  •  Tags:  
  • Related