用Python 3.3编写文件

用Python 3.3编写文件,python,python-3.3,Python,Python 3.3,有谁能给我写文件的建议吗。这是在Python3.3中实现的。这个错误消息只是不断弹出 回溯(最后一次调用):file.write中的第28行(name_1,“and”,“name_2”,“have a”,“loveness_2”,“坠入爱河的概率%))类型错误:write()只接受1个参数(给定6个) 我的代码是: if vowels_1 > vowels_2: loveness = vowels_2/vowels_1 loveness_2 = loveness

有谁能给我写文件的建议吗。这是在Python3.3中实现的。这个错误消息只是不断弹出

回溯(最后一次调用):file.write中的第28行(name_1,“and”,“name_2”,“have a”,“loveness_2”,“坠入爱河的概率%))类型错误:write()只接受1个参数(给定6个)

我的代码是:

  if vowels_1 > vowels_2:
      loveness = vowels_2/vowels_1
      loveness_2 = loveness * 100
      print("It is ",loveness_2,"% possible of you falling in love")
      print("*********************************************")
      file.write("*********************************************")
      file.write(name_1,"and",name_2,"have a",loveness_2,"percent chance of 
      falling in love")

file.write
print
不同;正如错误所说,它只需要一个参数。在将字符串传递给该调用之前,您需要编写字符串

一种方法是使用字符串格式:

line = "{} and {} have a {} percent chance of falling in love".format(name_1, name_2, loveness_2)
file.write(line)

逗号分隔参数,因此解释器认为您在这里给出了一系列参数。如果要进行字符串连接,请使用“+”

print('a' + 'b')
>>> 'ab'
一种更具python风格的方法是使用.format()


write()
需要多少个参数?你通过了多少次?提示:在写入可能重复的字符串之前连接字符串
print('{} some text {}'.format('foo', 'bar')
>>>'foo some text bar'