def main():
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'))
return main()
load() #to input the values
calc()
print()
我不确定我做错了什么:
我应该将变量名称放入def load():
,def calc():
和def print():
吗?
当我现在运行时,它表示"load"
未定义。我该如何定义load
?就此而言,如果我没有定义def calc():
和def print():
,我该如何定义它们?
我在代码末尾按照我要调用的顺序正确调用它们 - load
,calc
,然后print
。
是我的" return main()
"正确的做法?我不知道,我只是想让这些代码正常运行而不会出现任何错误。我的印象是,我只是遗漏了一些东西。任何帮助,将不胜感激。
答案 0 :(得分:1)
您在main = Main()
main.load()
范围内定义load()
。这意味着您无法使用main()
之外的功能。
简单的解决方案是你应该把你的函数定义为加载,计算和打印在main()
的定义之外(顺便说一句,把它称为main()
之类的东西,print已经是一个函数!)
您也不需要print_stock info
。我不确定你要做什么,但根本没有必要。
答案 1 :(得分:0)
让人惊讶。
你的第一个错误:在另一个函数中调用函数。也许你打算这样做
id +
然后你必须做
class Main:
def load(self):
#do a thing
你的第二个错误是定义一个新的print()函数,该函数使用打印函数,因为它已经存在。重命名它,因为这将导致巨大的错误
答案 2 :(得分:0)
这是一个简单的工作示例,用于说明如何解决您尝试做的事情。
# helper for interpreting input
import ast
#: initial starting values
thing, count, cost, total = 'Nothing', 0, 0, 0
# define all functions at module indentation
def get_user_input():
global thing, count, cost # write to the GLOBAL names thing, count and cost
thing = input('What have you?') # ask for a string
count = ast.literal_eval(input('How much do you have?')) # ask for a string and interpret it: "3.2" => float
cost = ast.literal_eval(input('How much does each cost?'))
def compute():
global total # write to global name total
total = count * cost # can read from global names
def pretty_print(): # not named print! we still need print to print!
print("You have", count, thing)
print("At", cost, "each, that makes", total, "in total")
# call functions at module indentation
get_user_input() # ducks, 3, 42
compute()
pretty_print()
# You have 3 ducks
# At 42 each, that makes 126 in total
有一件事要警告你:使用全局变量通常是一个坏主意。对于一个小脚本来说很好,但是你已经搞砸了基础知识 - 全局变量很棘手,所以尽可能避免使用它们。如果您只想运行这些命令,请不要将它们写为函数,直接执行它们。基本上,不要使用def ...
和global ...
行并将所有内容放在同一个缩进处,然后删除最后三行。
如果您真的想存储和打印多件物品,则需要使用容器。只需分配一个循环中的值,例如thing = input('What have you?')
会将其保留为输入的最后一个值。相反,您需要一个像list
这样的容器。然后,您可以append
添加其他值。
container = [] # [] is a list literal
for _ in range(3): # non-infinite loop
next_thing = ast.literal_eval(input('Place your order...\n'))
container.append(next_thing)
print("You ordered", container)
答案 3 :(得分:0)
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: "))
calc()
display()
name=input("\nEnter 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'))
def main():
load()
main()
print("\nTotal Profit is $", format(totalpr, '10,.2f'))