python,陷入无限循环程序

时间:2014-12-01 14:49:02

标签: python

我正在编写这个python程序,一切正常但是当我运行它时,我陷入无限循环。我如何解决它?谢谢。

import sys
def main():
    name = input("Enter your name:")
    ssn = input("Enter your Social Security Number:")
    income = eval(input("Enter your net income:"))

    while income <= 0:
        print("Error, then net income you entered is less than zero! Try again.")
        income = eval(input("Enter your net income again:"))

    while income > 0:
        net = calc(income, name, ssn)

def calc(income, name, ssn):
    if income > 15000.00:
        tax = .05 * (income - 15000.00)
        print(tax)
        print(name)
        print(ssn)
        sys.exit
    elif income > 30000.00:
        tax = .1 * (income - 30000.00)
        print(tax)
        print(name)
        print(ssn)
        sys.exit
    else:
        print("There is no tax to be paid, income is in the first $15000.00")
        print(name)
        print(ssn)
        sys.exit
main()

2 个答案:

答案 0 :(得分:2)

sys.exit是一个功能。您需要调用它(sys.exit())来退出脚本。

答案 1 :(得分:0)

不需要在main()中的第二个。 接下来就是&#34; sys.exit&#34;是不对的,你必须写&#34; sys.exit()&#34;。 在你的calc()函数中你应该替换if&#34;如果收入&gt; 15000.00:&#34;与&#34;如果收入&gt; 15000.00和收入&lt; = 30000:&#34;或者elif案件永远不会被考虑。

这里有正确的代码:

import sys

def main():
    name = input("Enter your name:")
    ssn = input("Enter your Social Security Number:")
    income = eval(input("Enter your net income:"))
    while income <= 0:
        print("Error, then net income you entered is less than zero! Try again.")
        income = eval(input("Enter your net income again:"))
    net = calc(income, name, ssn)

def calc(income, name, ssn):
    if income > 15000.00 and income <= 30000:
        tax = .05 * (income - 15000.00)
        print(tax)
        print(name)
        print(ssn)
        sys.exit()
    elif income > 30000.00:
        tax = .1 * (income - 30000.00)
        print(tax)
        print(name)
        print(ssn)
        sys.exit()
    else:
        print("There is no tax to be paid, income is in the first $15000.00")
        print(name)
        print(ssn)
        sys.exit()
main()