这是我创建的骰子游戏,并且不断收到以下错误消息:
> File "/Users/kc/PycharmProjects/programming project/dice game
> script/dice game.py", line 146, in <module>
> main() File "/Users/kc/PycharmProjects/programming project/dice game script/dice game.py", line 140, in main
> leaderboard = get_leaderboard() File "/Users/kc/PycharmProjects/programming project/dice game script/dice
> game.py", line 112, in get_leaderboard
> return [line.replace('\n', '') for line in l.readlines()]
io.UnsupportedOperation: not readable
我不知道为什么会出现此消息,我最初以为是因为“ Leaderboard.txt”被另存为w,所以我更改为r +文件,但消息不断出现,我认为它有事情要做与此:
return [line.replace('\n', '') for line in l.readlines()]
但是我不知道它是什么。(我在下面附加了完整的代码)
完整程序:
import pickle
import random
import time
print("")
print("welcome to the dice game")
print("")
with open('users.pickle', 'rb') as f:
users = pickle.load(f)
def login():
logged_in = False
username = input('please enter username: ')
while not logged_in:
if username in users:
password = input("enter password: ")
if password == users[username]:
print("access granted")
logged_in = True
else:
print("access denied")
return logged_in
print(" ")
score_p_one = 0
score_p_two = 0
rounds = 0
def roll():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
change = 10 if (die1 + die2) % 2 == 0 else -5
points = die1 + die2 + change
if die1 == die2:
points += random.randint(1, 6)
return points
def game():
player1_points = 0
player2_points = 0
for i in range(1, 6):
player1_points += roll()
print(f'After this round user1 you now have: {player1_points} Points')
time.sleep(1)
player2_points += roll()
print(f'After this round user2 you now have: {player2_points} Points')
time.sleep(1)
player1_tiebreaker = 0
player2_tiebreaker = 0
if player1_points == player2_tiebreaker:
while player1_tiebreaker == player2_tiebreaker:
player1_tiebreaker = random.randint(1, 6)
player2_tiebreaker = random.randint(1, 6)
player2_win = False
player1_win = (player1_points + player1_tiebreaker) \
# > (player2_points, player2_tiebreaker)
return (player1_points, player1_win), (player2_points, not player2_win)
def add_winner(winner):
f = open('Winner.txt', 'a')
with open('Winner.txt', 'a') as f:
f.write('{winner[0]},{winner[1]}\n')
下面是错误消息指向的地方
def get_leaderboard():
f = open('Leaderboard.txt', 'w')
with open('Leaderboard.txt', 'w') as l:
return [line.replace('\n', '') for line in l.readlines()]
def update_leaderboard(leaderboard, winner):
for idx, item in enumerate(leaderboard):
if item.split(', ')[1] == winner[1] and int(item.split(', ')[0]) < int(winner[0]):
leaderboard[idx] = '{}, {}'.format(winner[0], winner[1])
else:
pass
leaderboard.sort(reverse=True)
def save_leaderboard(leaderboard):
with open('Leaderboard.txt', 'r+') as f:
for item in leaderboard:
f.write(f"{item}\n")
def main():
user1 = login()
user2 = login()
(player1, player1_win), (player2, player2_win) = game()
if player1_win:
winner = (player1, user1)
else:
winner = (player2, user2)
print("Well done", winner[1], "you won with", winner[0] ,"Points")
add_winner(winner)
leaderboard = get_leaderboard()
update_leaderboard(leaderboard, winner)
save_leaderboard(leaderboard)
if __name__ == '__main__':
main()
答案 0 :(得分:1)
已解决:对不起,我很抱歉,这是一个简单的错误,没有为Leaderboard.text提供读写权限