python - Float must be a string or a number? -


i have simple program. code:

money = open("money.txt", "r") moneyx = float(money) print(moneyx) 

the text file, money.txt, contains this:

0.00 

the error message receive is:

typeerror: float() argument must string or number 

it simple mistake. advice? using python 3.3.3.

money file object, not content of file. content, have read file. if entire file contains 1 number, read() need.

moneyx = float(money.read()) 

otherwise might want use readline() read single line or try csv module more complex files.

also, don't forget close() file when done, or use with keyword have closed automatically.

with open("money.txt") money:     moneyx = float(money.read()) print(moneyx) 

Comments

Popular posts from this blog

html - height not adding height to a empty div -

angularjs - Redirect to a new template in angular js -

Oracle SQL. How to select specific IDs where value of columns specify criteria? -