Python 我试图写骰子游戏分数到一个文件,但我得到这样的类型错误。TypeError:write()参数必须是str,而不是tuple

Python 我试图写骰子游戏分数到一个文件,但我得到这样的类型错误。TypeError:write()参数必须是str,而不是tuple,python,Python,我在Python3.7.2中创建了一个骰子游戏,我需要将结果写入一个文本文件,但是在当前格式下,我得到了错误 我试着用一根弦来拉,但那只会引起更多的问题 file = open("dicegamescores.txt","w") x = str('on round',x,username1 ,'has',player1_score , '\n',username2'has', player2_score) file.write(x) file.close 我期望('on round',x

我在Python3.7.2中创建了一个骰子游戏,我需要将结果写入一个文本文件,但是在当前格式下,我得到了错误

我试着用一根弦来拉,但那只会引起更多的问题

file = open("dicegamescores.txt","w") 
x = str('on round',x,username1 ,'has',player1_score , '\n',username2'has', player2_score)
file.write(x) 
file.close

我期望('on round',x,username1,'has',player1_分数,'\n',username2,'has',player2_分数)根据迭代使用正确的变量值写入文件 但我明白了:

当不强制转换为STR时:

Traceback (most recent call last):
  File "C:\Users\joelb\AppData\Local\Programs\Python\Python37\dicegame.py", line 45, in <module>
    file.write(x)
TypeError: write() argument must be str, not tuple
Traceback (most recent call last):
  File "C:\Users\joelb\AppData\Local\Programs\Python\Python37\dicegame.py", line 44, in <module>
    x = str('on round', x, username1 , 'has', player1_score , '\n' , username2 , 'has', player2_score )
TypeError: str() takes at most 3 arguments (9 given)
回溯(最近一次呼叫最后一次):
文件“C:\Users\joelb\AppData\Local\Programs\Python\Python37\dicegame.py”,第45行,在
file.write(x)
TypeError:write()参数必须是str,而不是tuple
或者当我向STR投下:

Traceback (most recent call last):
  File "C:\Users\joelb\AppData\Local\Programs\Python\Python37\dicegame.py", line 45, in <module>
    file.write(x)
TypeError: write() argument must be str, not tuple
Traceback (most recent call last):
  File "C:\Users\joelb\AppData\Local\Programs\Python\Python37\dicegame.py", line 44, in <module>
    x = str('on round', x, username1 , 'has', player1_score , '\n' , username2 , 'has', player2_score )
TypeError: str() takes at most 3 arguments (9 given)
回溯(最近一次呼叫最后一次):
文件“C:\Users\joelb\AppData\Local\Programs\Python\Python37\dicegame.py”,第44行,在
x=str('在第二轮,x,用户名1,'has',玩家1_分数,'\n',用户名2,'has',玩家2_分数)
TypeError:str()最多接受3个参数(给定9个)

应该是这样的:

x = 'on round {round} {user1} has {user1score} \n {user2} has {user2score}'.format(
round = x, user1 = username1, user1score = player1_score, user2= username2, user2score = player2_score)

使用
.format()
方法,您可以在占位符中插入值,即
圆形

它应该是这样的:

x = 'on round {round} {user1} has {user1score} \n {user2} has {user2score}'.format(
round = x, user1 = username1, user1score = player1_score, user2= username2, user2score = player2_score)

使用
.format()
方法,您可以在占位符中插入值,即
round

当您试图以字符串的方式写入文件时,第一个占位符会失败。 第二个失败,因为(正如错误所说)您试图从未分组的元素中创建一个字符串。为了实现这一点,元素应该是onearray/list,以便Python可以从中生成适当的字符串:

x = str(['on round',x,username1 ,'has',player1_score , '\n',username2'has', player2_score]) #See the square brackets
然而,更具Python风格的方法是:

x = "on round %s %s has %d \n %s has %d" % (x, username1, player1_score, username2, player2score)

%s插入字符串,%d为整数,%f为浮点。请参阅,以获取有关此操作的解释。

当您尝试以字符串的方式向文件写入内容时,第一个操作失败。 第二个失败,因为(正如错误所说)您试图从未分组的元素中创建一个字符串。为了实现这一点,元素应该是onearray/list,以便Python可以从中生成适当的字符串:

x = str(['on round',x,username1 ,'has',player1_score , '\n',username2'has', player2_score]) #See the square brackets
然而,更具Python风格的方法是:

x = "on round %s %s has %d \n %s has %d" % (x, username1, player1_score, username2, player2score)

%s插入字符串,%d为整数,%f为浮点。请参阅以获取有关此操作的解释。

嘿,您需要将x变量设置为字符串格式。 您可以使用以下代码:

file = open("dicegamescores.txt","w") 
x = ('on round',2,username1 ,'has',player1_score , '\n',username2,'has', player2_score)
xx = ("%s%s%s%s%s%s%s%s%s")%(x)
file.write(xx) 
file.close

对于变量中的每个字符串,您都应该添加%s。嘿,您需要将x变量设置为字符串格式。 您可以使用以下代码:

file = open("dicegamescores.txt","w") 
x = ('on round',2,username1 ,'has',player1_score , '\n',username2,'has', player2_score)
xx = ("%s%s%s%s%s%s%s%s%s")%(x)
file.write(xx) 
file.close
对于变量中的每个字符串,您应该添加%s。

您必须
.format()
您的字符串;不强制转换。当您使用多个参数调用
str()
时,它会将每个参数转换为字符串,并将所有参数作为元组返回,而不会将它们连接起来。使用字符串格式。您必须
.format()
您的字符串;不强制转换。当您使用多个参数调用
str()
时,它会将每个参数转换为字符串,并将所有参数作为元组返回,而不会将它们连接起来。改为使用字符串格式。