• Support PF! Buy your school textbooks, materials and every day products Here!

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

  • Comp Sci
  • Thread starter opus
  • Start date
  • #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

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,941
5,660
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
opus
Gold Member
717
131
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
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,941
5,660
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
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,941
5,660
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
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
verty
Homework Helper
2,164
198
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
opus
Gold Member
717
131
Ok cool. Got it it work now. Thank you to everyone’s reply.
Now just to figure out the formatting justifications :mad:
 

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

Replies
3
Views
2K
  • Last Post
Replies
2
Views
730
Replies
6
Views
634
Replies
2
Views
438
Replies
4
Views
3K
Replies
14
Views
663
Replies
2
Views
529
Top