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

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Python programming issue related to type handling, specifically the error of using a string in arithmetic operations. Participants explore the implications of formatting functions and type conversions in the context of a program designed to calculate employee pay and deductions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant identifies a problem with a line of code where a string is unexpectedly being used in an arithmetic operation, leading to a type error.
  • Another participant suggests that Python's behavior may be similar to other languages, where formatting functions return strings regardless of the input type.
  • A participant mentions attempting to convert a string to a float to resolve the issue but encounters a problem with concatenation, indicating a misunderstanding of basic operations.
  • One reply clarifies that the format function does not round numbers but reformats them into strings, which may contribute to the participant's confusion.
  • Another beginner proposes a solution involving converting the string to a float before performing subtraction, acknowledging potential issues with significant digits in floating-point arithmetic.
  • A later reply emphasizes the need for decimal arithmetic over binary arithmetic to avoid inaccuracies in calculations, citing an example of floating-point addition.
  • One participant expresses uncertainty about the explanation due to their beginner status.
  • Another participant asserts that Python 3 automatically converts numbers to floats when necessary, suggesting that the format statement should not be a concern.
  • The original poster indicates that they resolved their issue but still need to address formatting concerns.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding Python's type handling and formatting functions. There is no consensus on the best approach to resolve the arithmetic issue, and multiple viewpoints on the implications of type conversion and formatting remain present.

Contextual Notes

Some participants highlight limitations in their understanding of Python's behavior and the nuances of floating-point arithmetic, which may affect their proposed solutions.

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: 460
  • 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: 455
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

Replies
55
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K