2014-12-01 5 views
-2

나는이 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

sys.exit은 기능입니다. 스크립트 (sys.exit())를 호출하여 스크립트를 종료해야합니다.

+0

덕분에 나는 내가있는 동안 수입> 0 문에 틈을 이용해 그것을 알아 낸 다음은 올바른 코드

. –

+0

그런데 어쨌든 왜 루프가 필요합니까? – Tomo

0

메인()에서 두 번째는 필요하지 않습니다. 다음은 "sys.exit"이 올바르지 않다는 것입니다. "sys.exit()"라고 써야합니다. calc() 함수에서 "if income> 15000.00 :"인 경우 "income> 15000.00 and income < = 30000 :"으로 바꾸거나 엘프 사례를 고려하지 않아야합니다.

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