输出为空白

时间:2016-10-12 06:08:04

标签: python function python-3.x calling-convention

def load():
    name=0
    count=0
    totalpr=0
    name=input("Enter stock name OR -999 to Quit: ")
    while name != '-999':
        count=count+1
        shares=int(input("Enter number of shares: "))
        pp=float(input("Enter purchase price: "))
        sp=float(input("Enter selling price: "))
        commission=float(input("Enter commission: "))
        name=input("Enter stock name OR -999 to Quit: ")

def calc():
    amount_paid=shares*pp
    commission_paid_purchase=amount_paid*commission
    amount_sold=shares*sp
    commission_paid_sale=amount_sold*commission
    profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
    totalpr=totalpr+profit_loss

def print():
    print("\nStock Name:", name)
    print("Amount paid for the stock:       $",      format(amount_paid, '10,.2f'))
    print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
    print("Amount the stock sold for:       $", format(amount_sold, '10,.2f'))
    print("Commission paid on the sale:     $", format(commission_paid_sale, '10,.2f'))
    print("Profit (or loss if negative):    $", format(profit_loss, '10,.2f'))
    print("Total Profit is $", format(totalpr, '10,.2f'))

def main():
    load()
    calc()
    print()

我想编写main()函数来调用它上面的函数。

然而,当我运行程序时,输出是空白的 - 没有 - 没有给出错误来解释问题。

我做错了什么?

3 个答案:

答案 0 :(得分:1)

除了使用内置功能的问题外,您也有范围问题。因此,您必须在每个函数global中定义变量,以便在其他函数中对它们进行评估。

def load():    
    global name
    global count
    global shares
    global pp
    global sp
    global commission
    name=input("Enter stock name OR -999 to Quit: ")
    count =0
    while name != '-999':
        count=count+1
        shares=int(input("Enter number of shares: "))
        pp=float(input("Enter purchase price: "))
        sp=float(input("Enter selling price: "))
        commission=float(input("Enter commission: "))
        name=input("Enter stock name OR -999 to Quit: ")

def calc():
    global amount_paid
    global amount_sold
    global profit_loss
    global commission_paid_sale
    global commission_paid_purchase
    global totalpr
    totalpr=0
    amount_paid=shares*pp
    commission_paid_purchase=amount_paid*commission
    amount_sold=shares*sp
    commission_paid_sale=amount_sold*commission
    profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
    totalpr=totalpr+profit_loss

def display():
    print("\nStock Name:", name)
    print("Amount paid for the stock:       $",      format(amount_paid, '10,.2f'))
    print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
    print("Amount the stock sold for:       $", format(amount_sold, '10,.2f'))
    print("Commission paid on the sale:     $", format(commission_paid_sale, '10,.2f'))
    print("Profit (or loss if negative):    $", format(profit_loss, '10,.2f'))
    print("Total Profit is $", format(totalpr, '10,.2f'))

def main():
    load()
    calc()
    display()

main()

答案 1 :(得分:0)

要将python模块作为程序运行,您应该像下面的那样运行它。在你的程序中,main就像其他函数一样,不会自动执行。

if __name__ == '__main__':
    load()
    calc()
    print()

我们正在做的是,我们检查模块名称是否为__main__,我们调用其他函数。仅当我们将模块作为主程序运行时才设置__main__

答案 2 :(得分:0)

您没有致电main()&还更改print()函数名称,此处我已将其更改为fprint()

def load():
    name=0
    count=0
    totalpr=0
    name=input("Enter stock name OR -999 to Quit: ")
    while name != '-999':
        count=count+1
        shares=int(input("Enter number of shares: "))
        pp=float(input("Enter purchase price: "))
        sp=float(input("Enter selling price: "))
        commission=float(input("Enter commission: "))
        name=input("Enter stock name OR -999 to Quit: ")

def calc():
    amount_paid=shares*pp
    commission_paid_purchase=amount_paid*commission
    amount_sold=shares*sp
    commission_paid_sale=amount_sold*commission
    profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
    totalpr=totalpr+profit_loss

def fprint():
    print("\nStock Name:", name)
    print("Amount paid for the stock:       $",      format(amount_paid, '10,.2f'))
    print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
    print("Amount the stock sold for:       $", format(amount_sold, '10,.2f'))
    print("Commission paid on the sale:     $", format(commission_paid_sale, '10,.2f'))
    print("Profit (or loss if negative):    $", format(profit_loss, '10,.2f'))
    print("Total Profit is $", format(totalpr, '10,.2f'))

def main():
    load()
    calc()
    fprint()
main()

编辑:更改了print()的函数名称