Python Python troubles- says I'm using a string in arithmetic

AI Thread Summary
The discussion revolves around a programming issue related to calculating employee pay, specifically the handling of data types in Python. The user is encountering an error indicating that a string is being used where a float is expected, particularly in the calculation of net pay. The confusion stems from the format function, which returns a string rather than a numeric type, leading to issues when performing arithmetic operations. Suggestions include converting the total deduction to a float before subtraction to avoid concatenation errors. Additionally, concerns are raised about the reliability of floating-point arithmetic in Python, emphasizing the need for proper handling of significant digits. Ultimately, the user resolves the initial issue but still seeks assistance with formatting the output correctly.
opus
Gold Member
Messages
717
Reaction score
131

Homework Statement


I'm making a program to display what I have posted in the image and my program needs to look just like it.
The idea is to have an employee enter information such as their pay and hours worked. Then taxes get calculated and it shows a net amount that the employee has made for the week.

Homework Equations

The Attempt at a Solution



If you look at line 17, I have underlined in yellow what is giving me problems.
In hovering over the text, the pop-up window reads: "Expected type 'float', got 'string' instead"

Why is it making it a string? That corresponding object is spelled out on line 16 and it's not a string there.
Any ideas?

Apologies if it's hard to understand what I'm doing with the code. I'm still very new and I'm sure it looks hideous to the more experienced type.
 

Attachments

  • Screen Shot 2018-09-29 at 6.29.33 PM.png
    Screen Shot 2018-09-29 at 6.29.33 PM.png
    46.3 KB · Views: 443
  • Screen Shot 2018-09-29 at 6.34.51 PM.png
    Screen Shot 2018-09-29 at 6.34.51 PM.png
    6.8 KB · Views: 439
Technology news on Phys.org
I don't know Python, but it's probably that it, like vb, for example, ALWAYS returns a string as the output of a format statement regardless of whether the thing being formatted is an integer, a float, a date, currency, a percent, etc.

When you get an unexpected result from a function, the obvious thing to do it to look at the definition of the function.
 
Ok that's goo to know, thank you.
So I've tried a couple of things.
First, I am unable to directly compute the totalDeduction by hand because it requires input values from the user. Additionally, I need the format function to round it off into a currency-appropriate number.
So for line 17, I tried using a float function on totalDeduction to make it not a string, but then it tells me that it can't concatenate a float. But I'm not trying to concatenate anything, I'm just trying to subract the two values.
 
opus said:
I need the format function to round it off into a currency-appropriate number.
? The format function doesn't round things off, it reformats them into strings. Yes, it produces strings that show the number of digits you specify, but that's formatting and not rounding. Presumably your concatenation problem is because you fail to understand the basic operations. Since I don't know Python I really can't help you. Not to worry, though, we have a lot of Python users here.
 
I am also a beginner but what about this:
line 17 netPay = grossPay - float(totalDeduction)

because the format function returns a string, I guess it might work if you change it into a float before subtracting it
 
Kittensrverycute said:
I am also a beginner but what about this:
line 17 netPay = grossPay - float(totalDeduction)

because the format function returns a string, I guess it might work if you change it into a float before subtracting it
Even if that works, it will not reliably solve the significant digits problem because floating a string and then doing arithmetic on it doesn't always give exactly the answer you think it does (*). You need to be doing decimal arithmetic but you're doing binary arithmetic.

(*) For example 1.34 + 1.16 is NOT going to give 1.50 --- it may sometimes seem to,depending on the software being used but in general the statement if 1.34 + 1.16 = 1.50 then A else B will give result B.
 
phinds said:
Even if that works, it will not reliably solve the significant digits problem because floating a string and then doing arithmetic on it doesn't always give exactly the answer you think it does (*). You need to be doing decimal arithmetic but you're doing binary arithmetic.

(*) For example 1.34 + 1.16 is NOT going to give 1.50 --- it may sometimes seem to,depending on the software being used but in general the statement if 1.34 + 1.16 = 1.50 then A else B will give result B.
Thanks for the reply
Sorry I don't quite understand I am new
 
I believe Python 3 automatically makes a number a float if it would truncate it otherwise. So you don't need to worry about the format statement, it'll become a float by itself.
 
Ok cool. Got it it work now. Thank you to everyone’s reply.
Now just to figure out the formatting justifications :mad:
 

Similar threads

Back
Top