Jump to content

Why is this not working?

TheGrim123321
Go to solution Solved by alexboz,

The input is being received as a string, you have to parse it as a float to do math on it. I don't really know python, but a quick google search leads me to this:

final_weight = (float(base_weight)-float(new_weight))

Try this and see if it works.

base_weight = input('Enter your previous weight: ')
new_weight = input('Enter your new weight: ')
final_weight = (base_weight-new_weight)
print (final_weight)

I get error  final_weight = (base_weight-new_weight)
TypeError: unsupported operand type(s) for -: 'str' and 'str' in pycharm.

 

I see no reason why I wouldn't work

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, fizzlesticks said:

You can't subtract strings.

ooooh ok thank you. I'm new to programming lol :)

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

The input is being received as a string, you have to parse it as a float to do math on it. I don't really know python, but a quick google search leads me to this:

final_weight = (float(base_weight)-float(new_weight))

Try this and see if it works.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, alexboz said:

The input is being received as a string, you have to parse it as an int to do math on it. I don't really know python, but a quick google search leads me to this:


final_weight = (float(base_weight)-float(new_weight))

Try this and see if it works.

Yep this worked thank you very much

TheGrim123321

CPU: I3-4170Cooler: Hyper TX3Mobo:Biostar B85MGMemory: G.Skill Ripjaw 2x4GBStorage: Barracuda 500GBGPU: Zotac GTX660Case: Fractal 1100PSU: Evga 500WMonitors: FHX2153L 21.5"│V193WEJb 19"Keyboard/Mouse: CMStorm DevastorAudio: MonoPrice 8323Microphone: BlueYeti BlackoutOS: MSX(Win10) Quote me or @TheGrim123321 to get my attention.
Link to comment
Share on other sites

Link to post
Share on other sites

So... basically the same issue as last time except with subtraction instead of addition. :P

 

Although I should probably point out that this time @alexboz suggested converting the user input to floats while I suggested converting them to integers. The main difference between them is that ints use less memory and don't support decimals while floats lose precision only when there are too many digits.

 

Anyway it's awesome that you're trying to learn learn how to code so keep it up!

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, ElfFriend said:

Anyway it's awesome that you're trying to learn learn how to code so keep it up!

 

My thoughts are ... less encouraging.

 

Two questions, same problem. Is getting given the answer from someone really helping with the learning process?

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, SSL said:

 

My thoughts are ... less encouraging.

 

Two questions, same problem. Is getting given the answer from someone really helping with the learning process?

Good point, but I think everyone should be encouraged to learn programming. Regardless of the kind of questions they may have or how many times they ask for help. Remember that we were all noobs at one point. A year ago I could have only "google programed" (searched up what I wanted and changed a few values), now I can write my own code without having to look up how to do it.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, ElfFriend said:

Good point, but I think everyone should be encouraged to learn programming.

 

Mm. That's certainly the politically correct attitude. For my part, I dislike dealing with code written by people who suck at it.

 

Critical thinking and problem solving should really be what is emphasized, not "programming".

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SSL said:

 

Mm. That's certainly the politically correct attitude. For my part, I dislike dealing with code written by people who suck at it.

 

Critical thinking and problem solving should really be what is emphasized, not "programming".

Agreed, and while it does help knowing how to use google, stack overflow, critical thinking and problem solving; I don't expect a beginner to have the knowledge required to know how to go about solving a problem.

 

Meaning that I'll be more than willing to help a beginner with problems which had already been solved than an intermediate/advance programmer. Since a beginner wouldn't necessarily know how their problem is connected to other similar problems (yes this topic's issue seems obvious to us but think in beginner terms here) while an advance programmer should be able to tell what problems are similar and have the knowledge required to solve them in the event that they have them.

 

Essentially, while I think that @TheGrim123321 should try to solve these on his own before asking for help, I don't think we shouldn't encourage him to keep at it when he does come to us for aid.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×