【发布时间】: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