简体   繁体   English

为什么我的代码中的分数总是 0?

[英]Why does the score in my code is always 0?

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.它需要从 rating.txt 中读取之前的分数,并在文件中添加 50 分以防平局和 100 分以防万一获胜。 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')

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'.您不是在阅读和添加分数,您只是将新分数附加到 rating.txt,这是因为您以“a”而不是“w”开头。

Therefore change all the a to w and change因此将所有 a 更改为 w 并更改

win = 0

to

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

And that should work.这应该有效。

This is because your win variable in loop is being updated to 0 every time.这是因为您在循环中的win变量每次都被更新为 0。 You need to first set it to the current score then, you need to add to it and update the file.您需要先将其设置为当前分数,然后,您需要添加它并更新文件。

Your code is good but what you have to do is just open your your file rating.txt in a write mode (w) not apend mode and also define win veriable out of loop.您的代码很好,但您要做的就是以写入模式(w)而不是追加模式打开您的文件 rating.txt,并在循环外定义 win veriable。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM