1

The program I'm writing is to use a given formula to calculate financial assistance for families that loops back and asks the user if they want to do it for another family. Part of the instructs was also to use -1 as a sentinel value for the input. Here is what I have at the moment:

def main():
cont = "y"
while cont.lower() == "y":
    try:
        income = float(input("Please enter the annual household income:"))
        children = int(input("How many children live in the house?"))
        amountAssistance = assistanceTotal(income, children)
        print("For a household making", income, "per year with", children, " children the amount of assistance would be", amountAssistance)
        cont = input("Would you like to run this again? (Y/N)")
    except:
        print("Something went wrong.")
        print("Did you enter a whole number of children?")
        cont = input("Would you like to try again? (Y/N)")

def assistanceTotal(money, kids):
    if (money >= 30000 and money <= 40000) and kids >= 3:
        moneyTotal = 1000 * kids
        return moneyTotal
    if (money >= 20000 and money <= 30000) and kids >= 2:
        moneyTotal = 1500 * kids
        return moneyTotal
    if money < 20000:
        moneyTotal = 2000 * kids
        return moneyTotal
main()

I've tried looking up info on using sentinel values with loops like this but I haven't been able to quite grasp if how I'm thinking of using it is correct. For this type of a situation would I need one sentinel value or two, one for income and one for children? I think I would also need to alter my cont statements and change how it reacts if they type in a -1. Any help is appreciated even if it is just directing me to accurate info on how to use sentinels.

Edit to add another sort of follow up question: Do sentinel values have to be numbers or are they most commonly numbers? What is the difference between having a continue portion using yes/no inputs to using sentinel values? For an example: In my text book it shows using a sentinel to stop the loop by asking for either an input of an integer or enter -1 to exit, is this not the same thing as asking continue Y/N?

OzzyPyro
  • 47
  • 1
  • 7

2 Answers2

0

If you want all of the inputs to be able to detect the sentinel value, you can use a custom input function that raises a RuntimeError (or a custom exception of your own) and handle the exception in a uniform way.

def main():
    def get_input(prompt):
        i = input(prompt)
        if i == '-1':
            raise RuntimeError()
        return i
    cont = "y"
    while cont.lower() == "y":
        try:
            income = float(get_input("Please enter the annual household income:"))
            children = int(get_input("How many children live in the house?"))
            amountAssistance = assistanceTotal(income, children)
            print("For a household making", income, "per year with", children, " children the amount of assistance would be", amountAssistance)
            cont = input("Would you like to run this again? (Y/N)")
        except RuntimeError:
            print("Done.")
            break
        except:
            print("Something went wrong.")
            print("Did you enter a whole number of children?")
            cont = input("Would you like to try again? (Y/N)")
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Generally you would use the sentinel to exit the loop, in this case entering -1 for income, e.g.:

while True:
    try:
        income = int(input("Please enter the annual household income (-1 to exit):"))
        if income == -1:
            break
        children = int(input("How many children live in the house?"))
        amountAssistance = assistanceTotal(income, children)
        print("For a household making", income, "per year with", children, " children the amount of assistance would be", amountAssistance)
    except ValueError:
        print("Something went wrong.")
        print("Did you enter a whole number of children?")

NB 1: given the sentinel is -1 and all of the tests for income are against ints I would suggest keeping income an int.
NB 2: it is best to catch explicit exceptions, e.g. ValueError vs a catch-all except: clause

AChampion
  • 29,683
  • 4
  • 59
  • 75