0

I want to use a function as a String, i have this function:

def getCoins(user):     ##Get the Coins from a user.##
    try:
        with open(pathToUser + user + ".txt") as f:
            for line in f:
                print(user + " has " + line +" coins!")
    except:
        print("Error!")

Now it just print the coins, but i want to use it in other codes like this:

client.send_msg(m.text.split(' ')[2] + "has" + coinapi.getCoins('User') + "coins")

How do it do that? So that i can use it like a string, the message in Twitchchat should be:

"USERXYZ has 100 coins"

2 Answers2

1

Return a string

def getCoins(user):     ##Get the Coins from a user.##
    try:
        with open(pathToUser + user + ".txt") as f:
            return '\n'.join(user+" has "+line +" coins!" for line in f)
    except:
        return "Error!"

Also you should use format strings (assuming python 3.6)

def getCoins(user):     ##Get the Coins from a user.##
    try:
        with open(f"{pathToUser}{user}.txt") as f:
            return '\n'.join(f"{user} has {line} {coins}!" for line in f)
    except:
        return "Error!"
ddg
  • 1,090
  • 1
  • 6
  • 17
0

You should be able to just return the string you want in the output. It looks like your code is iterating over f and printing the number of coins on each line, so you may need to return a list or generator of the coins amount, but the key answer to your question is just to return a string or something that can be turned into a string

pscheidler
  • 141
  • 3