我试图比较 int 和 func,它给出了一个错误



我试图比较函数和一个整数,但它给出了一个错误 我 game.py:

def game():
return 64
score = game()
with open("hiscore.txt" , "r") as f:
hiscore = int(f.read)
if  hiscore<score :
with open("hiscore.txt" , "r") as f:
f.write(str(score))

我的希比特.txt

34

我的控制台:

hiscore = int(f.read)
TypeError: int() argument must be a string, a bytes-like object or a number, not 
'builtin_function_or_method'

你在f.read之后错过了()

int(f.read())

感谢您在以下代码中指出写入文件时的错误模式

with open("hiscore.txt" , "r") as f:
f.write(str(score))

并且您应该将写入文件的模式从open("hiscore.txt" , "r")更改为"w""a"

  • "r":读取文件内容
  • "w":覆盖任何现有内容
  • "a":将附加到行尾

相关内容

最新更新