带while循环的前哨值

时间:2018-07-04 22:24:05

标签: python python-3.x while-loop sentinel

我正在编写的程序是使用给定的公式来计算向家庭循环的财务资助,并询问用户是否要为另一个家庭这样做。部分指令还使用-1作为输入的标记值。这是我目前所拥有的:

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()

我曾尝试查找有关将哨兵值与此类循环一起使用的信息,但我仍无法完全掌握我想如何使用它。对于这种情况,我需要一个或两个定点值,一个用于收入,一个用于儿童?我认为我还需要更改我的cont语句,并更改它们在输入-1时的反应方式。感谢您提供任何帮助,即使它只是引导我获得有关如何使用前哨的准确信息。

编辑以添加另一种后续问题: 前哨值必须是数字还是最常用的数字?使用yes / no输入的连续部分与使用前哨值的区别是什么?举个例子:在我的课本中,它显示了使用哨兵通过请求整数输入或输入-1退出来停止循环,这与要求继续Y / N不同吗?

2 个答案:

答案 0 :(得分:0)

如果希望所有输入都能够检测到前哨值,则可以使用引发RuntimeError(或您自己的自定义异常)的自定义输入函数,并统一处理该异常方式。

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)")

答案 1 :(得分:0)

通常,您会使用sentinel退出循环,在这种情况下,请为-1输入income,例如:

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?")

注意1:假设前哨是-1,并且对income的所有测试都针对int,我建议保持incomeint
注意2:最好捕获明确的异常,例如ValueError与全面的except:子句