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

In summary, the programmer is attempting to solve a problem with a function by looking at the definition of the function and trying different things. However, he is not able to reliably solve the significant digits problem and he is also a beginner.
  • #1
opus
Gold Member
717
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: 391
  • 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: 383
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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
 
  • #6
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.
 
  • #7
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
 
  • #8
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.
 
  • #9
Ok cool. Got it it work now. Thank you to everyone’s reply.
Now just to figure out the formatting justifications :mad:
 

1. Why am I getting an error saying I'm using a string in arithmetic in Python?

This error occurs when you are trying to use a string in a mathematical operation, such as addition or multiplication. Python treats strings as text, not numbers, so they cannot be used in arithmetic operations.

2. How can I fix the "string in arithmetic" error in Python?

To fix this error, you can convert the string to a number using the int() or float() function. This will allow you to perform mathematical operations on the string.

3. Can I use a string in arithmetic in Python in any situation?

No, strings cannot be used in arithmetic in Python in any situation. They can only be used in mathematical operations if they are converted to numbers first.

4. What other errors can occur when using strings in arithmetic in Python?

Another common error is the "unsupported operand type" error, which occurs when you try to use a string with an unsupported arithmetic operator, such as division or exponentiation.

5. Is there a way to avoid the "string in arithmetic" error in Python?

Yes, you can avoid this error by making sure to use numerical data types (integers or floating-point numbers) instead of strings when performing arithmetic operations in Python.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
8
Views
875
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Replies
3
Views
98
Back
Top