简体   繁体   中英

I tried to compare int and func and it gives an error

I tried to compare function and an integer but it gives an error my 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))

my hiscore.txt

34

my console:

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

You missed () after f.read .

int(f.read())

Thanks for ルカスpointing out the wrong mode when writing to file in following code

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

And you should change the mode for writing to file from open("hiscore.txt", "r") to "w" or "a"

  • "r" : read the contents of the file
  • "w" : overwrite any existing content
  • "a" : will append to the end of the line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM