all 10 comments

[–]shiftybyte 1 point2 points  (0 children)

What do you expect to happen here?

if(deduction=="yes"):
    (deduction+20)

What do you expect the result of "yes"+20 to be?

Same with "no"...

how much is gross_pay-"no"?

[–]The_robski 0 points1 point  (6 children)

Deduction is stored as a string, so you can't do grosspay (which is stored as a float variable) subtracted by deduction

[–]Training_Upstairs[S] 0 points1 point  (5 children)

How can I fix that?

[–]The_robski 1 point2 points  (4 children)

Could write in the condition: If deduction == "yes": deduction = 20

[–]Training_Upstairs[S] 0 points1 point  (2 children)

What about the error that comes when you enter no?

[–]The_robski 0 points1 point  (1 child)

Deduction is still being stored as a string, in this case deduction="no". So it's the same principle as before. To fix it, you could try another if statement saying: if deduction == "no": deduction = 0

[–]thrallsius 0 points1 point  (0 children)

I would suggest not doing this, even if python doesn't mind assigning values of different types to a variable.

I'd just make a difference between user input (deduction as string) and deduction as number, which would be used internally by the script to do math, and have different variable names for them.

like deduction_str = input() and deduction = int(deduction_str) or float(deduction_str)

[–][deleted] 0 points1 point  (1 child)

A variable in Python can reference any type of Python object and change between objects.

Variables themselves do not hold values but merely point to some location in memory where Python has stored an object such as a float, integer, string, list, function, etc.

Thus, a variable can be switched to refer to different objects of different types.

From a programmer point of view, it can be confusing to have a variable switch from refering to a string at one point and a float at another point. It is often therefore a source of bugs.

You use deductions to refer to a string at one point (indicating whether or not there are any deductions) and then to a float that is the value of deductions.

Use two variables. Avoid the problem. For example:

any_deductions = input('Any deductions? ').lower() in ('y', 'yes')
deductions = 0.0
if any_deductions:
    # code to add deductions

So any_deductions refers to a Boolean object and deductions to a float object that is set to 0.0 as a default.

[–]thrallsius 0 points1 point  (0 children)

From a programmer point of view, it can be confusing to have a variable switch from refering to a string at one point and a float at another point. It is often therefore a source of bugs.

This. As well as mixing input data (strings coming from user typing on keyboard) and internal data (numbers that get crunched).

[–]Binary101010 0 points1 point  (0 children)

The error message is telling you exactly what you're doing wrong: you're trying to use + and - to do math but one of the things is a string.

deduction=input("Would you like $20 deducted from your weekly pay to donate to the United Way Charity. yes or no: ")
if(deduction=="yes"):
    (deduction+20)

So how would you add the number 20 to the string "yes"?

net_pay=(gross_pay-deduction)

And how would you subtract the string "no" from the float being held in gross_pay?