我的python程序有一些问题

时间:2015-03-09 23:59:09

标签: python

下面我有我的结算脚本,当我运行它时,我得到TypeError

# the main function
def main():
    endProgram = 'no'
    endProgram = raw_input('Do you want to end program? (Enter no or yes): ')

    #declare Variables
    priceOfParts = 0
    hours = 0
    shopRate = 0

    #Function calls
    tax=(taxes)
    taxableTotal(priceOfParts, hours, shopRate)
    totalBill(taxableTotal, taxes)
    outputTotalBill(totalBill)

#Password Function
count = 0
while count < 3:
    password = raw_input('Please enter a password: ')
    if password != 'spyderPC':
        count = count + 1;
    print 'You have entered invalid password %i times.' % (count)
    if count == 3:
        print 'Access Denied'
        break
    else:
        print 'Access Granted'
        break

# hours function
def hours(hours):
    hours = input("hours worked")
    while hours <0 or hours >80:
        print 'Hours cannot be a negative or over 80 per job'
    return hours

# shop Rate function
def shopRate(shopRate):
    shopRate = input("Shop rate")
    while shopRate<0:
        print 'Shop rate cannot be a negative'
    return shopRate

#Price of parts function
def priceOfParts(priceOfParts):
    priceOfParts = input("total Price of Parts")
    return priceOfParts

#taxable total Function
def taxableTotal(hours, shopRate, priceOfParts):
    taxableTotal = float (hours) * shopRate + priceOfParts
    return taxableTotal

# Calculate Taxes function
def taxes(taxableTotal ):
    taxes= float(taxeableTotal)*.08
    return taxes

# calculate total bill
def totalBill(taxableTotal, tax):
    totalBill = taxableTotal + taxes
    return totalBill

#out put bill to file
def outPut_totalBill(hours, shopRate, priceOfParts , taxableTotal, taxes, totalBill):
    outFile = open('Bill.txt', 'a')
    print >> outFile, 'The bill for services is $'
    outFile.write('hours' + '\n')
    outFile.write('shopRate'+ '\n')
    outFile.write('priceOfParts' + '\n')
    outFile.write('taxableTotal' + '\n')
    outFile.write('taxes' + '\n')
    outFile.write('totalBill' + '\n')
    outfile.close

main()

当它运行时,我得到了这个:

>>> Please enter a password: spyderPC
>>> You have entered invalid password 0 times.
>>> Access Granted
>>> Do you want to end program? (Enter no or yes): no

但是,我收到了这些错误:

Traceback (most recent call last):
  File "C:\Users\school pc\Desktop\myProgram.py", line 89, in  <module>
   main()
  File "C:\Users\school pc\Desktop\myProgram.py", line 20, in main
   totalBill(taxableTotal, taxes)
  File "C:\Users\school pc\Desktop\myProgram.py", line 74, in totalBill
   totalBill = taxableTotal + tax
TypeError: unsupported operand type(s) for +: 'function' and 'function'

1 个答案:

答案 0 :(得分:0)

这是一个清理版本:

from   __future__ import print_function
import sys

if sys.hexversion < 0x3000000:
    # Python 2.x
    inp = raw_input
else:
    # Python 3.x
    inp = input

def get_login(max_tries=3):
    for try_ in range(max_tries):
        password = inp('Please enter a password: ')
        if password == 'spyderPC':
            print("Access granted")
            return True
    else:
        print("You have entered an incorrect password {} times.".format(max_tries))
        return False

def get_number(prompt, low=None, high=None, type_=float):
    while True:
        try:
            number = type_(inp(prompt))
            if low is not None and number < low:
                print("Value must be >= {}".format(low))
            elif high is not None and high < number:
                print("Value must be <= {}".format(high))
            else:
                return number
        except ValueError:
            pass

def get_yn(prompt, default=None, yeses={'y','yes'}, noes={'n','no'}):
    while True:
        s = inp(prompt).strip().lower() or default
        if s in yeses:
            return True
        elif s in noes:
            return False

def get_hours(max_hours=80):
    return get_number("Hours worked: ", 0., max_hours)

def get_shop_rate():
    return get_number("Shop rate: ", 0.)

def get_parts_price():
    return get_number("Total price of parts: ", 0.)

def calc_taxes(before_taxes, tax_rate=0.08):
    return before_taxes * tax_rate

def write_bill(outf):
    # get inputs
    shop_rate    = get_shop_rate()
    hours        = get_hours()
    parts        = get_parts_price()
    # calculate results
    labor        = shop_rate * hours
    before_taxes = labor + parts
    taxes        = calc_taxes(before_taxes)
    after_taxes  = before_taxes + taxes
    # write results
    outf.write(
        "\n"
        "The bill is\n"
        "  Hours    {hours:6.2f}  h\n"
        "  @Rate  $ {shop_rate:6.2f} /h\n"
        "                    == {labor:8.2f}\n"
        "  Parts                {parts:8.2f}\n"
        "                     ----------\n"
        "                       {before_taxes:8.2f}\n"
        "                + Tax  {taxes:8.2f}\n"
        "                     ----------\n"
        "               Total $ {after_taxes:8.2f}\n"
        .format(**locals())
    )

def main():
    print("Create a bill!")

    if get_login():
        with open("Bill.txt", "a") as outf:
            while True:
                write_bill(outf)
                if not get_yn("Do another bill? [Y/n] ", default='y'):
                    break

if __name__=="__main__":
    main()

产生类似

的输出
The bill is
  Hours     18.00  h
  @Rate  $  65.00 /h
                    ==  1170.00
  Parts                  231.46
                     ----------
                        1401.46
                + Tax    112.12
                     ----------
               Total $  1513.58

The bill is
  Hours     12.00  h
  @Rate  $  88.00 /h
                    ==  1056.00
  Parts                  451.19
                     ----------
                        1507.19
                + Tax    120.58
                     ----------
               Total $  1627.77