我正在尝试编写一个猜数字游戏,但我不断得到随机错误,似乎无法找到我做错了什么。这是脚本:
import random
import sys
import os
def guess_number():
file = open('highscores.txt', 'r+')#opens file
highscore = file.read()
highscore = int(highscore)
name = input('Enter your name: ')
answer = random.randint(1,100)
print('Hi ' + name + '! I\'m thinking of a number between 1 and 100!')
guesses = 0
while guesses < 100:
print('Take a Guess! ')
guess = input()
guess = int(guess)
if guess < answer:
print('Your guess was too low!')
guesses = guesses + 1
if guess > answer:
print('Your guess was too High!')
guesses = guesses + 1
if guess == answer:
guesses = guesses + 1
break
if guess == answer:
answer = str(answer)
guesses = str(guesses)
print('Good Job ' + name + '! You guessed ' + answer + ' in ' + guesses + ' guesses!')
guesses = int(guesses)
if highscore > guesses:
file.close()
os.remove('highscores.txt')
file = open('highscores.txt', "w")
print('Congratulations! You beat the highscore!')
guesses = str(guesses)
file.write(guesses)
file.close()
guesses = int(guesses)
elif highscore < guesses:
highscore = str(highscore)
print('The highscore is: ' + highscore)
顺便说一下,我有一个名为highscores.txt
的单独文件;当程序第一次运行时,我在文件中输入“100”并保存。
这是我得到的错误的追溯:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
guess_number()
File "C:\Users\rhino_000\Desktop\guesses\guesses.py", line 39, in guess_number
os.remove('highscores.txt')
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'highscores.txt'
答案 0 :(得分:0)
这个问题的答案是你应该在再次使用之前关闭文件。在Python中执行此操作的一种简单方法是使用with
语法。
with open('qwerty.txt', 'r') as f:
content = f.read()
# File is automatically closed now.
print(content)
问题很可能是您连续多次运行程序,但elif
部分没有关闭文件,并且程序停止时文件未在OS级别关闭。您应该始终在Python中关闭该文件,以免在系统中获得此类挂钩。
答案 1 :(得分:0)
这是基于makaveli的回答和我在上面发表的评论。下面的代码似乎解决了所有文件系统访问问题。我已经多次测试游戏,看起来效果很好。
import random
def guess_number():
with open('highscores.txt', 'r+') as f:
highscore = int(f.read())
name = input('Enter your name: ')
answer = random.randint(1, 100)
print('Hi ' + name + '! I\'m thinking of a number between 1 and 100!')
guesses = 0
while guesses < 100:
print('Take a Guess! ')
guess = input()
guess = int(guess)
if guess < answer:
print('Your guess was too low!')
guesses = guesses + 1
if guess > answer:
print('Your guess was too High!')
guesses = guesses + 1
if guess == answer:
guesses = guesses + 1
break
if guess == answer:
answer = str(answer)
guesses = str(guesses)
print('Good Job ' + name + '! You guessed ' + answer + ' in ' + guesses + ' guesses!')
guesses = int(guesses)
if highscore > guesses:
print('Congratulations! You beat the highscore!')
with open('highscores.txt', "w") as f:
guesses = str(guesses)
f.write(guesses)
elif highscore < guesses:
highscore = str(highscore)
print('The highscore is: ' + highscore)
guess_number()
运行示例
以下是我所做的一次测试运行的示例。在我运行测试之前,highscores.txt
包含9(来自之前的运行),并且在运行之后它包含6。
Enter your name: tagc
Hi tagc! I'm thinking of a number between 1 and 100!
Take a Guess!
50
Your guess was too High!
Take a Guess!
25
Your guess was too low!
Take a Guess!
37
Your guess was too low!
Take a Guess!
44
Your guess was too High!
Take a Guess!
40
Your guess was too High!
Take a Guess!
38
Good Job tagc! You guessed 38 in 6 guesses!
Congratulations! You beat the highscore!