1

So i have these defined:

def highscorenumber():
    file = open('GUESS THE NUMBER HIGHSCORE NUMBER.txt', 'r')
    highscorenum = file.readline(1)

def highscorename():
    file = open('GUESS THE NUMBER HIGHSCORE NAME.txt', 'r')
    highscorenum = file.readline(1)

And these are saved in the same directory as the program. "GUESS THE NUMBER HIGHSCORE NUMBER.txt" when opened says: "1"

And "GUESS THE NUMBER HIGHSCORE NAME.txt" when opened says "max".

Yet when I run:

print("The current highscore is",highscorenumber,"set by",highscorename)

it says:

The current highscore is <function highscorenumber at 0x0000000002D46730> set by <function highscorename at 0x0000000001F67730>

Why does it say this instead of "The current highscore is 1 set by max"?

2 Answers 2

3

Because you are not calling the functions, Python is printing representations of the function objects themselves:

>>> def f():
...     return 1
...
>>> print(f)
<function f at 0x015E1618>
>>> print(f())
1
>>>

As demonstrated above, you need to call the functions in order to print their return values:

print("The current highscore is",highscorenumber(),"set by",highscorename())

You should also be returning the call to readline from each function:

def highscorenumber():
    file = open('GUESS THE NUMBER HIGHSCORE NUMBER.txt', 'r')
    return file.readline(1)

Otherwise, the functions will return None by default.


Finally, I'd use with-statements to open the files:

def highscorenumber():
    with open('GUESS THE NUMBER HIGHSCORE NUMBER.txt', 'r') as file:
        return file.readline(1)

This will ensure that they are closed when you are done with them.

Sign up to request clarification or add additional context in comments.

Comments

-2

Use camel case... Its the way python reads things. Camel case is where there is no spaces in the names... Also, don't forget to close the file

for example the name would be:

GuessTheNumberHighscoreName.txt

or

Guess_The_Number_HighScore_Name.txt

1 Comment

1) CamelCase is more specific than that and doesn't generally use underscores. 2) Python uses a variety of naming conventions for different sorts of objects. 3) What you name a file is completely irrelevant to whether or not Python can open it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.