导入pygame,随机
def quiteGame():
readFile = open("BestScore.txt")
bestScoreFile = readFile.read()
readFile.close()
writeFile = open("BestScore.txt")
iScore = max(score,int(bestScoreFile))
print('Your Score is :', score)
print('Highest Score is :', iScore)
writeFile.write(str(iScore))
writeFile.close()
pygame.quit()为什么会有消息“writeFile.write(str(iScore)) io.UnsupportedOperation: not writable”显示在那里。
发布于 2021-03-01 15:01:15
为了写入文件,你需要在“可写”模式下打开它。默认情况下,python以“只读”模式打开任何.txt文件
要以可写模式打开文件,请执行以下操作:
writeFile = open("BestScore.txt","w") #Opens BestScore.txt in writable mode
[...]#Other code因此,最终的代码应该如下所示:
import pygame, random
def quiteGame():
readFile = open("BestScore.txt")
bestScoreFile = readFile.read()
readFile.close()
writeFile = open("BestScore.txt","w")
iScore = max(score,int(bestScoreFile))
print('Your Score is :', score)
print('Highest Score is :', iScore)
writeFile.write(str(iScore))
writeFile.close()
pygame.quit()https://stackoverflow.com/questions/66414244
复制相似问题