user10918292
user10918292

Reputation:

How to store the gametag and score of the player into an external .txt file?

i am trying to export the gametag and score into an external file for a school project, but keep getting the error that write() argument must be a str, not tuple...sorry if basic mistakes are made I'm very new to this.

import random
import os
import sys
import time

def cls():
    os.system('cls' if os.name=='nt' else 'clear')
score = 0
y=0
user = ["fab", "joshua", "charlie"]
username = input("Username: ")
username = username.lower()
if username in user:
    print("***You have been verified***")
    print("*****Music quiz game by joshua wiley*****")
    time.sleep(2)
    cls()
    print("---------------------------------------------")
    gametag = str(input("Gamertag: "))
    while y != 1:
        x = int(0)
        points = 3
        randomsong = random.choice(open('songs.txt').readlines())
        names = randomsong.split(",")
        print("The artist is: " + names[1])
        songname = (names[0])
        song = songname.split()
        letters = [word[0] for word in song]
        print("The first letters of each word within the title are: " + "     ".join(letters))
        print(song)
        while x < 2:
            x = x+1
            guess = input("What is your guess: ")
            guess = guess.upper()
            if guess == songname:
                score = score + points
                print("Well done you are correct!")
                x=3
            elif guess != song:
                points = points - 2
                print("Sorry that is incorrect!")
        if x == 2:
            score = str(score)
            print("You have failed with " + score + " points!")
            score = int(score)
            y = 1
        time.sleep(1)
        cls()
    score = str(score)
    scores = open("scores.txt", "a")
    line = (gametag,score)
    scores.write(line)
    print(scores)

    sorted(scores, key=int, reverse=True)
    top5 = scores[:5]

    print(top5)

else:
    print("You have not been verified.")

Picture of error message:

screenshot showing error

Upvotes: 0

Views: 90

Answers (3)

Alex Yu
Alex Yu

Reputation: 3547

You're trying to

line = (gametag,score)
scores.write(line)

So indeed you are trying to write tuple.

You forgot to transform your tuple into something write-able.

E.g.: scores.write(f"(line)\n") or something similar

Upvotes: 0

Erland Huam&#225;n
Erland Huam&#225;n

Reputation: 56

Your "line" variable is being declared as a tuple, the error is telling you exactly that so instead of: line = (gametag,score) it should be line = gametag + score and it should work, (not sure) didn't have the time to test it out for myself.

Upvotes: 1

Ricky Kim
Ricky Kim

Reputation: 2022

Like the error says, you're trying to write a tuple into text file. Try writing a str instead. For example:

Maybe change line = (gametag,score) to line='{},{}'.format(gametag, score)

Upvotes: 1

Related Questions