[Tutor] Is this correct syntax for what I want?

Nathan Pinno falcon3166 at hotmail.com
Sun Jun 25 10:02:31 CEST 2006


When I loaded it up, I got the following error:

Traceback (most recent call last):
  File "C:\Python24\Account Tracker.py", line 82, in ?
    load_file(accountlist)
  File "C:\Python24\Account Tracker.py", line 10, in load_file
    amount = line.next().strip()
AttributeError: 'str' object has no attribute 'next'

According to it, the 'str' object has no attribute 'next'. So how would I 
load my file containing my data?

Relevant code:
def load_file(ac):
    import os
    filename = 'accounts.txt'
    if os.path.exists(filename):
        store = open(filename, 'r')
        for line in store:
            account = line.strip()
            amount = line.next().strip()
            ac[account] = amount
    else:
        store = open(filename, 'w')
    store.close

Thanks.
Nathan Pinno
----- Original Message ----- 
From: "Alan Gauld" <alan.gauld at freenet.co.uk>
To: "Nathan Pinno" <falcon3166 at hotmail.com>; <tutor at python.org>
Sent: Sunday, June 25, 2006 1:07 AM
Subject: Re: [Tutor] Is this correct syntax for what I want?


>>  File "C:\Python24\Account Tracker.py", line 49, in printall
>>    print account,"\t $",accountlist[account]+"\n"
>> TypeError: unsupported operand type(s) for +: 'float' and 'str'
>>
>> So how do I fix this error?
>
> What it's saying is you can't add a float and string. If you look at the 
> code you are trying to add "\n" to accountlist[account] which is a float. 
> You need to convert the string to a float or the float to a string. In 
> this case converting the float to a string would be the better approach! 
> :-)
>
> However I would personally recommend that you use a format string here 
> since it will give you much better control of the appearance of the output 
> and avoids the need to convert the values.
>
> print "%s\t $%0.2f\n" % (account, accountlist[account])
>
> HTH,
>
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
> 


More information about the Tutor mailing list