2016-10-12 3 views
0
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

내장 된'print()'함수를 오버라이드하고 있습니다 ... 다른 이름을 사용하십시오 – qxz

+0

변경했습니다. 아직 아무 일도 일어나지 않았다. –

+1

또한 범위 문제가 있습니다. 각 함수에 정의 된 모든 변수는 다른 변수의 범위를 벗어납니다. –

답변

0

파이썬 모듈을 프로그램으로 실행하려면, 다음과 같이 실행해야합니다. 귀하의 프로그램에서 main은 다른 기능과 동일하며 자동으로 실행되지 않습니다. 모듈 이름이 __main__이며, 우리가 다른 함수를 호출하는 경우 우리가 뭐하는

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

, 우리는 확인한다. __main__은 모듈을 주 프로그램으로 실행할 때만 설정됩니다. 당신은 main() &도 print() 함수 이름을 변경 호출되지 않습니다

+0

두 번 등호에 오류가 나타났습니다. –

+0

@FrostyElsa가 코드를 수정했습니다. – thavan

0

가, 여기에 내가 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() 

편집으로 변경되었습니다 내장 사용의 문제와 함께 인쇄()

+0

감사합니다. 그러나 : 지금, 나는 루프에 갇혀 인쇄물을 호출하지 않을 것이다. –

+0

인쇄 기능 이름을 변경하십시오. – trahane

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

입력 한 후 표시되지 않습니다. 주식 정보. 대신 주식 정보를 입력하고 "-999"를 입력하면 주식 정보가 "-999"라는 이름으로 인쇄됩니다. 반면에 커미션을 입력하면 즉시 계산 한 후 표시 할 수 있습니다. –

관련 문제