Python TypeError:写入文件需要一个整数(获取类型str)
我正在做一个骰子游戏,我把赢家和分数写进同一文件夹中的一个txt文件 我已尝试将usernameone和Username2转换为str:Python TypeError:写入文件需要一个整数(获取类型str),python,string,types,integer,Python,String,Types,Integer,我正在做一个骰子游戏,我把赢家和分数写进同一文件夹中的一个txt文件 我已尝试将usernameone和Username2转换为str: else: if playeronepoints>playertwopoints: print("player one has won") file=open("winners.txt","a","\n") file.write(usernamefirst,playereonepoints)
else:
if playeronepoints>playertwopoints:
print("player one has won")
file=open("winners.txt","a","\n")
file.write(usernamefirst,playereonepoints)
file.close()
else:
print("player two has won")
file=open("winners.txt","w","\n")
file.write(usernamesecond,playertwopoints)
file.close()
应写入文件,但获取类型错误:需要一个整数获取类型str问题是您调用打开:
以下是该方法的签名:
openfile,mode='r',buffering=-1,encoding=None,errors=None,newline=None,closefd=True,opener=None
只需使用openfile“r”
您还应该考虑使用上下文管理器:
使用openfile,“r”作为fh: 书面形式我知道了,你导入操作系统了吗?是的,那就不行了。。。 如果您使用的是函数form OS不包括所有内容,请仅包括您需要的软件包,例如您使用的是name and system,然后使用这种语法:
from os import system, name
这是因为操作系统有另一个叫做open的函数,我认为它与int类型有关,而与您想要使用的open无关
希望这有帮助。这并不能回答这个问题。一旦你有足够的钱,你将能够;相反这并不能真正回答问题。如果您有不同的问题,可以单击以提问。一旦你有足够的时间,你也可以吸引更多的注意力-