ckhgray
ckhgray

Reputation: 101

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

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)

Upvotes: 3

Views: 45640

Answers (4)

Irtaza Tariq
Irtaza Tariq

Reputation: 1

Instead of using .write() function, use the .writelines() function with square braces. Then change count to str format, and you'll be done:

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

Upvotes: 0

Rupesh
Rupesh

Reputation: 21

Add + before "\n"

That means your code will look like

hisc = open("highscore.txt", "a+")
hisc.write(count+"\n")

Upvotes: 2

ckhgray
ckhgray

Reputation: 101

@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")

Upvotes: 3

C14L
C14L

Reputation: 12558

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.

Upvotes: 7

Related Questions