-1

Can anyone give me advice on writing in files. This is in python 3.3. This error message just keeps on popping up.

Traceback (most recent call last):Line 28, in file.write(name_1,"and",name_2,"have a",loveness_2,"percent chance of falling in love") TypeError: write() takes exactly 1 argument (6 given)

And my code is this:

  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")
  • How many arguments does `write()` take? And how many did you pass? Hint: concatenate your strings before writing – Chris_Rands Mar 02 '18 at 10:20
  • Possible duplicate of [Python error TypeError: function takes exactly 1 argument (5 given)](https://stackoverflow.com/questions/23766383/python-error-typeerror-function-takes-exactly-1-argument-5-given) – Chris_Rands Mar 02 '18 at 10:23

2 Answers2

1

file.write is not the same as print; as the error says, it only takes a single argument. You need to compose your string before passing it to that call.

One way to do that is with string formatting:

line = "{} and {} have a {} percent chance of falling in love".format(name_1, name_2, loveness_2)
file.write(line)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

A comma separates arguments, so the interpreter thinks you're giving a bunch of arguments here. If you want to do a string concatenation, use '+'.

print('a' + 'b')
>>> 'ab'

A more pythonic way would be to use .format()

print('{} some text {}'.format('foo', 'bar')
>>>'foo some text bar'
uwain12345
  • 356
  • 2
  • 21