r/learnpython • • Sep 23 '19

New to python, do I have to force conversion everytime I want to use a variable?

Hey redditors,

I am playing around with python and I want to create a program that determines how long it will take me to save a certain amount, but I notice that I get a typeError even though I have converted everything to float.

Here is the link to the code: calc1.py

1 Upvotes

8 comments sorted by

2

u/VirulentCitrine Sep 23 '19

OP, I'm not getting any errors when I run your code on my end, so perhaps check your editor's settings?

The other user said you should switch your code from:

rent = input()
rent = float(rent)

to

rent = float(input())

and that is true, as it is a better practice, but it shouldn't cause you any errors either way.

Also, I just have to say that when I clicked on your third example file, I almost fell off my chair laughing because of how random it was when I read "I will now count my chickens!" lmao. Not making a joke at you, I just wasn't expecting that.

1

u/Usual_Mistake Sep 23 '19

LOL i see. anyways, i have fixed the code. Now, I'm trying to figure the math logic. hehe.

1

u/[deleted] Sep 23 '19

There's like six scripts in this link. Can you be more specific so we don't have to read your mind?

1

u/Usual_Mistake Sep 23 '19

Sorry I thought it would link to the specific file, it is for calc1.py

2

u/[deleted] Sep 23 '19
float(biweekly_earnings)

You haven't converted anything to anything, you've just made a float and then thrown it away. You have to assign the value:

biweekly_earnings = float(biweekly_earnings)

And yes, input returns only strings so if you use to to collect numeric values, you have to convert them to numeric types.

1

u/Usual_Mistake Sep 23 '19

Thank you, I thought I could modify it like that.

1

u/[deleted] Sep 23 '19

Typically functions are going to operate by return value, not manipulate their values in-place. (In any case, both strings and floats are immutable so there would be no way for float to operate in-place.)

2

u/Phillyclause89 is u/SoNotRedditingAtWork Sep 23 '19

You’re not assigning your converted variables to a namespace. Instead of

rent = input()
float(rent)
...

Try:

rent = input()
rent = float(rent)

Or:

rent = float(input())