Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: TypeError: write() takes exactly one argument (2 given)

Tags:

python

count = 100

start = input("Welcome to Reign of Terror \n 1-Start Game 2-Highscores")
if start == "2":
    hisc = open("highscore.txt", "a+")
    hisc.write(count,"\n")

When I run the code and choose 2, I get the error

TypeError: write() takes exactly one argument (2 given)
like image 804
ckhgray Avatar asked Dec 17 '25 12:12

ckhgray


2 Answers

Write takes one string, not two.

if start == "2":
    with open("highscore.txt", "a+") as hisc:
        hisc.write("{}\n".format(count))

Also, use with so your file gets closed after writing.

like image 125
C14L Avatar answered Dec 19 '25 00:12

C14L


@kabanus answered this, thanks!

It says exactly what the error is. Pass a single argument hisc.write(str(count)+"\n") will work." – kabanus

So this is the correct code:

hisc.write(str(count)+"\n")
like image 21
ckhgray Avatar answered Dec 19 '25 02:12

ckhgray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!