Float sayings it a string & cant do math with it

I’m trying to create a varible turn it to a float do math with it & store and write with it aswell, but it gives me an error saying its a list

Please enter your code as text. Like this we cannot copy/paste from it, nor will people using a screen reader be able to read it.

From the documentation:

readlines ( hint=-1 )

Read and return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint .

So MadeTotal is a list. It may have one item, but f.readlines will always return a list.

As an aside, it is customary in python to use snake case for variable and function names. made_total would be more “normal” in a Python program.

TypeError: readlines() takes no keyword arguments

MadeTotal = 0
MadeNew = input("How much money did you make this week: ")
MadeNew = float(MadeNew)
MadeTotal = float(MadeTotal)
MadeTotal = float(MadeTotal)

EMadeMonthly = MadeNew * 4
EMadeMonthly = float(EMadeMonthly)

TaxsPer = input (“Tax Percentage: “)
TaxsPer = float(TaxsPer)
ETaxs = TaxsPer / 100
ETaxs = float(ETaxs)
TaxRed = EMadeMonthly * ETaxs
TaxRed = float(TaxRed)
MadeWeek = MadeNew - TaxRed
MadeWeek = float(MadeWeek)
print (“Total made this week {}”.format(float(MadeWeek)))
with open(“MoneyData.txt”) as f: # in read mode, not in write mode, careful
MadeTotal = f.readlines(hint=-1)
print (MadeTotal)
MadeTotal = float(MadeTotal)
print (MadeTotal)
MadeTotal = MadeNew + MadeTotal - TaxRed
with open(“MoneyData.txt”, “w”) as f: # in write mode
f.write(”{}”.format(MadeTotal))

EMadeMonthly = EMadeMonthly - TaxRed
print (“Monthly Estimate {}”.format(float(EMadeMonthly)))

Bills = input (“Any bills to add: “)
Bills = float(Bills)
BillsDed = EMadeMonthly - Bills
BillsDed = float(BillsDed)
print (“You need to save {}”.format(float(Bills)) +” for bills this month & can spend {}”.format(float(BillsDed)))
MadeTotal = str(MadeTotal)
print (“Total made this month: {}”.format(str(MadeTotal)))

To format your code see:

It is not the hint I was aiming at, but the return type. You do not need the hint, it is defaulted.

A list of lines is not a float, even if the list contains one line with a float. Python is a language with strong typing, so it will not magically change the type of the output of readlines() to a float. You printed the MadeTotal and it printed ['337.02']. That is the output for a list, containing one item and it is a string. You can call float(MadeTotal[0]), i.e. on the first item of the list, not on the list.

Can you give me an example
Also i dont understand how to format my code if its in here or in python, the article isnt well written.

MadeTotal = 0
MadeNew = input("How much money did you make this week: ")
MadeNew = float(MadeNew)
MadeTotal = float(MadeTotal)
MadeTotal = float(MadeTotal)

EMadeMonthly = MadeNew * 4
EMadeMonthly = float(EMadeMonthly)

TaxsPer = input (“Tax Percentage: “)
TaxsPer = float(TaxsPer)
ETaxs = TaxsPer / 100
ETaxs = float(ETaxs)
TaxRed = EMadeMonthly * ETaxs
TaxRed = float(TaxRed)
MadeWeek = MadeNew - TaxRed
MadeWeek = float(MadeWeek)
print (“Total made this week {}”.format(float(MadeWeek)))
with open(“MoneyData.txt”) as f: # in read mode, not in write mode, careful
MadeTotal = f.readlines(float(MadeTotal))
print (MadeTotal)
MadeTotal = float(MadeTotal)
print (MadeTotal)
MadeTotal = MadeNew + MadeTotal - TaxRed
with open(“MoneyData.txt”, “w”) as f: # in write mode
f.write(”{}”.format(MadeTotal))

EMadeMonthly = EMadeMonthly - TaxRed
print (“Monthly Estimate {}”.format(float(EMadeMonthly)))

Bills = input (“Any bills to add: “)
Bills = float(Bills)
BillsDed = EMadeMonthly - Bills
BillsDed = float(BillsDed)
print (“You need to save {}”.format(float(Bills)) +” for bills this month & can spend {}”.format(float(BillsDed)))
MadeTotal = str(MadeTotal)
print (“Total made this month: {}”.format(str(MadeTotal)))

On discourse you can preserve the code formatting by either indenting it
with 4 spaces, or putting it inside triple backticks:

```
code here
```

Your code below looks ok though.

Regarding your problem, this code:

with open("MoneyData.txt") as f:
    MadeTotal = f.readlines(float(MadeTotal))

The readlines method takes no parameters. I am guessing from your code
that “MoneyData.txt” is a file with a floating point value written as
text, one per line. And that you are trying to total those values.

This:

f.readlines()

returns all the lines of text from the file as a list.
However, you need to sum the floats written on those lines.

A more normal way to do this is line this:

MadeTotal = 0
for line in f:
    value = float(line.strip())
    MadeTotal += value

That processes the file one line at a time. line.strip() removes leading
and trailing whitespace, in particular the trailing newline included
with each line of text. And float() then tries to convert what remains
as a floating point value. FOr example, a line might be “1.10\n” as read
from the file. line.strip() is “1.10” and float(“1.10”) is 1.1.

This has two advantages:

  • it avoids any trouble converting the list of strings from readlines()
    into individual lines
  • it one loads a single line into memory ata time. FOr a small file this
    isn’t very important, but finanical data routinely contains very any
    lines.

I’m concerned by this:

with open("MoneyData.txt", "w") as f:  # in write mode
    f.write("{}".format(MadeTotal))

That looks like it overwrites the original file with a single line of
text. That surprises me - it destroys the original. I would normally
expect it to write to a different file.

I would also do this outside the first “with open()”, not inside.

Cheers,
Cameron Simpson cs@cskk.id.au

If you are posting from the website, format your code with the button </>.

If you are posting by email, you can either:

  1. Indent the code with four spaces at the start of each line.

  2. Or put it between three backticks like this:

    code goes here
    

The backticks must be on lines of their own.

What article are you talking about?

You have this line repeated twice:

MadeTotal = float(MadeTotal)
MadeTotal = float(MadeTotal)

Delete one of the lines.

Then you have this:

EMadeMonthly = MadeNew * 4
EMadeMonthly = float(EMadeMonthly)

The second line is not needed, EMadeMonthly is already a float. Same
applies here:

ETaxs = TaxsPer / 100
ETaxs = float(ETaxs)

and all the other places you do a mathematical calculation and then
follow it with a call to float(). If the calculation works, the call
to float is unnecessary because it is already a float. If the
calculation doesn’t work, converting to a float afterwards is too late.

You need the call to float after the input lines because input returns
a string, not a number. Even if it contains only digits, it is still not
a number until you call float.

with open("MoneyData.txt") as f:  # in read mode, not in write mode, careful
    MadeTotal = f.readlines(hint=-1)

Get rid of the hint=-1 part, you don’t need it and it doesn’t work
that way. All you need is:

with open("MoneyData.txt") as f:  # in read mode, not in write mode, careful
    MadeTotal = f.readlines()

but readlines returns a list of strings, not a total. So this is a
better way:

with open("MoneyData.txt") as f:  # in read mode, not in write mode, careful
    WeeklyTotals = []  # Or will they be monthly totals???
    for line in f:  # read one line at a time
        WeeklyTotals.append(float(line))

Or something like that, I don’t know exactly what you are supposed to do
with the MoneyData file. You will need to read your assignment
instructions and think about what you need to do with each line.

When you have do it, move these line:

with open("MoneyData.txt", "w") as f:  # in write mode
    f.write("{}".format(MadeTotal))

out one level: they are indented inside the previous with open
block. You have this:

with open("MoneyData.txt") as f:
    code in here...
    with open("MoneyData.txt", 'w') as f:
        code here too...

That’s wrong and will cause errors. You need this:

with open("MoneyData.txt") as f:
    code in here...

with open("MoneyData.txt", 'w') as f:
    code here too...

Indentation is important in Python.

Hope that helps.