功能没有定义,虽然它被定义了......走向疯狂

时间:2017-04-19 07:01:04

标签: python

所以我对python很新,并且已经开始和关闭这个任务已经工作了一周,并且不能让它正常运行。我现在得到的错误告诉我函数get_in_code没有定义,虽然我已经定义了它。任何帮助将不胜感激!

SENTINEL = 'XXX'
DAY_CHARGE = 1500.00
#Define get_days
        def get_days():
            good_data = False
            while not good_data:
                try:
                    n_days = int(input("Please enter the number of days you stayed: "))
                except ValueError:
                    print("Error, Bad Data")
                else:
                    if n_days > 0:
                    good_data = True
                else:
                    print("This is bad data, please re enter data")
            return n_days

        #define get_cost(p)
        def get_cost():
            cost = float(input("Please enter the cost for the procedures: "))
                while cost < 0:
                    print("Procedure cost cant be negative: ")
                    cost = float(input("Please enter the cost for the procedures: "))
                return cost

        #define med cost
        def med_cost():
            med_cost = float(input("Enter the cost of your medicine: "))
                while med_cost < 0:
                    print("Medicine cost cant be negative: ")
                    med_cost = float(input("Enter the cost of your medicine: "))
                return med_cost
        #Find day cost
        def find_day_cost(in_code, n_days):
            day_cost = n_days * DAY_CHARGE
            if in_code == 'ZH':
                p_day_cost = day_cost * 0.20
                in_day_cost = day_cost *0.80
            elif in_code == 'HH':
                p_day_cost = day_cost * 0.10
                in_day_cost = day_cost * 0.90
            elif in_code == 'CH':
                p_day_cost = day_cost * 0.25
                in_day_cost = day_cost * 0.75
            else:
                p_day_cost = day_cost
                in_day_cost = 0
                return p_day_cost, in_day_cost

        #find procedure cost
        def find_proc_cost(in_code, cost):
            if in_code == 'ZH':
                p_proc_cost = 0
                in_proc_cost = cost
            elif in_code == 'HH':
                p_proc_cost = cost * 0.10
                in_proc_cost = cost * 0.90
            elif in_code == 'CH':
                p_proc_cost = cost * 0.50
                in_proc_cost = cost * 0.50
            else:
                p_proc_cost = cost
                in_proc_cost = 0
            return p_proc_cost, in_proc_cost

        #find medicine cost
        def find_med_cost(in_code, med_cost):
            if in_code == 'ZH':
                p_med_cost = 0
                in_med_cost = med_cost
            elif in_code == 'HH':
                p_med_cost = med_cost * 0.10
                in_med_cost = med_cost * 0.90
            elif in_code == 'CH':
                p_med_cost = med_cost * 0.50
                in_med_cost = med_cost * 0.50
            else:
                p_med_cost = med_cost
                in_med_cost = 0
                return p_med_cost, in_med_cost

       #Display pat_info
        def display_pat_info(pat_name, in_name):
            print("City Hospital - Patient Invoice")
            print("Patient Name: ", pat_name)
            print("Insurance: ", in_name)

        #display day cost
        def display_day_cost(p_day_cost, in_day_cost):
            print("Patient Day Cost: ", p_day_cost,"\tInsurance Day Cost: ", in_day_cost)

        #display procedure cost
        def display_proc_cost(p_proc_cost, in_proc_cost):
            print("Patient Procedure Cost: ", p_proc_cost, "\tInsurance Procedure Cost: ",   in_proc_cost)

        #display medicine cost
        def display_med_cost(p_med_cost, in_med_cost):
            print("Patient Medicine Cost: ", p_med_cost, "\tInsurce Medicine Cost: ", in_med_cost)

        #Display totals
        def display_totals(total_pat, total_in):
            print("Total Billed To Patient: ", total_pat, "\tTotal Billed To Insurance: ",   total_in, "\tTotal Bill: ", (total_pat + total_in))

        #display day_totals
        def display_day_totals(total_zip, total_happy, total_cheap, total_pat):
            print("City Hospital - End Of Day Billing Report")
            print("Total Dollar Amount Billed Today: ",        total_zip+total_happy+total_cheap+total_pat)
            print("Total Billed To Zippy Healthcare: ", total_zip)
            print("Total Billed To Happy Healthcare: ", total_happy)
            print("Total Billed To Cheap Healthcare: ", total_cheap)
            print("Total Billed To Uninsured: ", total_pat)

            #display day_counts()
            def display_day_counts(zip_count, happy_count, cheap_count, no_in_count):
            print("The total amount of Zippy Healthcare patients is: ", zip_count)
            print("The total amount of Happy Healthcare patients is: ", happy_count)
            print("The total amount of Cheap Healthcare patients is: ", cheap_count)
            print("The total amount of Uninsured patients is: ", no_in_count)
#def main 
def main():

    #Counters and accumulators
    total_zip= 0.00
    total_cheap= 0.00
    total_happy= 0.00
    total_pat= 0.00
    zip_count= 0
    cheap_count= 0
    happy_count= 0
    no_in_count= 0
    total_in = 0

    #Open file
    try:
        Pat_File = open('PatientBill.txt', 'w')
    except ValueError:
        print("*****ERROR***** - Corrupt File")
    else:
        file_exist = True

    #Priming read
    pat_name = input("Please enter the patients name: (XXX to stop program)")

    #Processing loop
    while pat_name != SENTINEL:
        #Input data 
        in_code = get_in_code()
        num_days = get_days()
        proc_cost = get_cost()
        med_cost = med_cost()

        #find each cost
        pat_day, insure_day = find_day_cost(in_code, num_days)
        pat_proc, insure_proc = find_proc_cost(in_code, proc_cost)
        pat_med, insure_med = find_med_cost(in_code, med_cost)

        #update accumulators and totals
        total_pat += pat_day + pat_proc + pat_med
        if in_code == 'ZH':
            zip_count += 1
            total_zip += in_day_cost + in_proc_cost + in_med_cost
            in_name = 'Zippy Healthcare'
        elif in_code == 'HH':
            happy_count += 1
            total_happy += in_day_cost + in_proc_cost + in_med_cost
            in_name = 'Happy Healthcare'
        elif in_code == 'CH':
            cheap_count += 1
            total_cheap += in_day_cost + in_proc_cost + in_med_cost
            in_name = 'Cheap Healthcare'
        else:
            no_in_count += 1
            in_name = 'Uninsured'
            total_in = total_zip + total_happy + total_cheap
        #displays patients invoice
        display_pat_info(pat_name,in_name)
        display_day_cost(pat_day, insure_day)
        display_proc_cost(pat_proc, insure_proc)
        display_med_cost(pat_med, insure_med)
        display_totals(pat_day + pat_proc + pat_med, insure_day + insure_proc +   insure_med)

        #Write output to file
        if file_exist:
            Pat_File.write(pat_name, pat_day+pat_med+pat_proc )

        #Get next patients name
        pat_name = input("Please enter the patients name: (XXX to stop program)")
        #Close the output file
        if file_exist:
            Pat_File.close()

        #display the accumlators and totals
        display_day_totals(total_zip, total_happy, total_cheap, total_pat)
        display_day_counts(zip_count,happy_count,cheap_count,no_in_count)
        #define get_in_code
        def get_in_code():
            in_code = input("Please enter one of the insurance codes, ZH, CH, HH, XX")
        while in_code not in ('ZH', 'HH', 'CH', 'XX'):
            print("***Please enter a proper insurance code***")
            in_code = input("Please enter one of the insurance codes, ZH, CH, HH, XX")
        return in_code

main()

1 个答案:

答案 0 :(得分:0)

Python是一种解释性语言。您需要在使用之前定义函数。只需移动文件顶部的函数定义即可。

问题还在于缩进。您只是在while语句后面的return内定义了方法。实际功能根本没有定义 - 解释器没有到达那些行。

此外,代码是“脏”的。将代码拆分为单独的类/模块。创建明确定义的责任区域,这将改进代码,并且更容易使用它。