【问题标题】:Why does the score in my code is always 0?为什么我的代码中的分数总是 0?
【发布时间】:2022-01-05 17:05:25
【问题描述】:

我已经编写了这段代码,但它不起作用,它不计算分数。它需要从 rating.txt 中读取之前的分数,并在文件中添加 50 分以防平局和 100 分以防万一获胜。然后读取文件并告诉玩家他们的分数。这是我的代码,请帮助我:

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

【问题讨论】:

  • 当你使用调试器时,first 行没有达到你的预期?
  • 因为您一直将其设置为 0 并且从未真正从文件中读取分数。

标签: python file arguments file-writing


【解决方案1】:

这是因为循环中的 win 变量每次都被更新为 0。您需要先将其设置为当前分数然后,您需要添加到它并更新文件。

【讨论】:

  • 抱歉,我的最后一条评论发给了错误的用户。
【解决方案2】:

您不是在阅读和添加分数,您只是将新分数附加到 rating.txt,这是因为您以“a”而不是“w”开头。

因此将所有 a 更改为 w 并更改

win = 0

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

这应该可行。

【讨论】:

    【解决方案3】:

    您可以将所有分数添加到列表中,最后对所有值求和。

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

    【讨论】:

    • 我已删除文件操作并将其用例替换为您可以打印以检查所有分数的列表。
    【解决方案4】:

    您的代码很好,但您只需在写入模式 ('w') 而非附加模式 ('a') 下打开文件 rating.txt,并在循环外定义 win 变量。

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多