开发者

Simple function error

code:

date=int(raw_input("Date:"))
ammount=int(raw_input("Ammount:"))
desc=str(raw_input("Description:"))
account=str(raw_inp开发者_C百科ut("Account:"))

def addEntry(date, ammount, desc, account):
    transact=open("transactions.txt", "w")
    transact.write(date, ammount, desc, account)
    transact.close()

addEntry(date, ammount, desc, account)

gives

Traceback (most recent call last):
  File "C:\tbank.py", line 11, in <module>
    addEntry(date, ammount, desc, account)
  File "C:\tbank.py", line 8, in addEntry
    transact.write(date, ammount, desc, account)
TypeError: function takes exactly 1 argument (4 given)

how can i make it work?


date=int(raw_input("Date:"))
ammount=int(raw_input("Ammount:"))
desc=str(raw_input("Description:"))
account=str(raw_input("Account:"))

def addEntry(date, ammount, desc, account):
    transact=open("transactions.txt", "w")
    transact.write('%s, %s, %s , %s' % (date, ammount, desc, account))
    transact.close()

addEntry(date, ammount, desc, account)


You are opening a file to write to it. It takes a single string as argument.

transact.write("your string")

Since all you are doing is write to file. You can avoid conversion. And raw_input returns a string.

date=raw_input("Date:")
amount =raw_input("Ammount:")
desc=raw_input("Description:")
account=raw_input("Account:")

You can add all of them as one string, before writing it to file


As others have noted, you must pass write a single string argument. There is another way to write things out to an open file that should be mentioned: the special form of print

print >>transact, date, amount, desc, account

This directs print to output to the file that immediatedly follows the '>>' It behaves exactly like print normally does, so it will write out all the values separated by a space, with a newline at the end (which you can suppress by adding a trailing comma).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜