2017-12-22 17 views
0

として、私は文字列としての機能を使用したいが、私はこの機能を持っている:Pythonの利用機能文字列

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

は今、それだけでコインを印刷し、しかし、私はこのような他のコードでそれを使用したい:

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

どうすればよいですか? Twitchchatのメッセージは文字列のように使うことができるので、

"USERXYZ has 100 coins" 

答えて

1

戻りますが、フォーマット文字列を使用する必要があります。また

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!" 

文字列(仮定の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!" 
に変換することができます
0

出力に必要な文字列を返すことができます。 あなたのコードがfを繰り返して、各行にコインの枚数を印刷しているようですので、コインの金額のリストまたはジェネレータを返す必要があるかもしれませんが、あなたの質問に対する主な答えは文字列か何かを返すことですそれは、文字列

関連する問題