-4

I'm needing help on getting the loop in my function Deductions to work.

I've tried looking through similar problems on stackoverflow but struggling to get my head around them.

def Deductions(money, Tax, TotalInsurance):

    deductions = 0
    global TotalDed
    TotalDed = 0
    choice = ""

    while not choice == "Y" or choice == "N":
        try:
            choice = str(input("Do you want to add any more deductions to your income, e.g car, rent or mortgage? Y/N : "))

        except ValueError:

            print("Must enter Y or N")

        if choice == "Y":

            while choice == "Y":

                AddDed = int(input("How much would you like to deduct: "))

                deductions = AddDed + deductions

                loop = str(input("Would you like to add more deductions? Y/N: "))

            if loop == "Y":
                choice == "Y"

            elif loop == "N":

                choice =="N"

        elif choice == "N":

            TotalDed = TotalTax + deductions


    print("Income: £", money)
    print("Taxed: £", Tax)
    print("National Insurance: £", TotalInsurance)
    print("Other Deductions: £", deductions)
    print("Total Deductions: £", TotalDed)

    return TotalDed

I'm wanting to make sure that my loop only accepts "Y" and "N". then to proceed to ask for Deductions.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Sayse May 09 '19 at 08:29
  • 3
    `not choice == "Y" or choice == "N"` is equivalent to `(not choice == "Y") or (choice == "N")`. `or` has a higher precedence than `not`. Try `choice not in ('Y', 'N')` instead. – Klaus D. May 09 '19 at 08:32

1 Answers1

0

I think there are several mistakes in your code :

As pointed out in the comments, from what I understand that you're trying to do, you should use while not (choice == "Y" or choice == "N").

You seem to have forgotten TotalTax = Tax + TotalInsurance.

try/except will not throw out a ValueErrorfrom input, so what you're looking for is probably an else clause after if and elif.

choice == "Y" is a boolean, it does not set a value. You're looking for choice = "Y".

I think you're confused when you're using the choice variable in a second while loop and then use loop to set a value to choice. Below is another structure I would choose for what you're trying to do.

You can also add more protection against the possible wrong values from input statements.

To sum this up, here is what I think you should write :

def Deductions(money, Tax, TotalInsurance):

    deductions = 0
    global TotalDed
    TotalDed = 0
    TotalTax = Tax + TotalInsurance
    choice = ""

    while choice != "N":
        choice = input("Do you want to add any more deductions to your income, e.g car, rent or mortgage? Y/N : ")

        if choice == "Y":
            AddDed = float(input("How much would you like to deduct: "))
            deductions = AddDed + deductions

        elif choice != "N":
            print("Must enter Y or N")

    TotalDed = TotalTax + deductions
    print("Income: £", money)
    print("Taxed: £", Tax)
    print("National Insurance: £", TotalInsurance)
    print("Other Deductions: £", deductions)
    print("Total Deductions: £", TotalDed)

    return TotalDed

Also

AddDed = float(input("How much would you like to deduct: "))
deductions = AddDed + deductions

can be replaced with

valid_added_value = False
while not valid_added_value:
    try:
        AddDed = float(input("How much would you like to deduct: "))
        valid_added_value = True
    except ValueError:
        print('Must be a numerical value')
deductions = AddDed + deductions

for extra protection, because it could throw a ValueError.

Also you don't need str in front of input because input already returns a str object in python3.

I'm not sure either why you need global TotalDed since you already return it but maybe you have a good reason.

Ashargin
  • 498
  • 4
  • 11