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()函数来调用它上面的函数。你知道吗
但是,当我运行程序时,输出是空的-没有-没有给出错误来说明问题。你知道吗
我做错什么了?你知道吗
除了使用内置函数的问题外,还存在范围问题。因此,必须使每个函数中定义的变量
global
,以便可以在其他函数中对它们进行评估。你知道吗您没有调用
main()
,也没有更改print()
函数名,这里我将其更改为fprint()
编辑:更改print()的函数名
要将python模块作为程序运行,您应该像下面这样运行它。在您的程序中,main和其他函数一样,不会自动执行。你知道吗
我们要做的是,检查模块名是否为
__main__
,并调用其他函数。__main__
仅当我们将模块作为主程序运行时才设置。你知道吗相关问题 更多 >
编程相关推荐