我是python的新手,我还在学习。我花了几个小时但我想出了如何做功能,但是我很难让用户输入他们想要输入的数量。输出只能工作一次,我确定这是因为for循环被分成两个函数,但我似乎无法弄清楚for循环是否仅进入负载,如果是这样会影响def calc():?
def main():
num_stocks,name,numsh,pp,sp,comm=load()
Amtpaid,bycomm,stocksold,scomm,profit=calc(num_stocks,name,numsh,pp,sp,comm)
prnt(Amtpaid,bycomm,stocksold,scomm,profit)
def load():
#load inputs
num_stocks=int(input("How many stocks do you want processed?"))
for i in range (num_stocks):
print("Stock number",i+1)
print(".....................")
name=input("Enter stock name:\n")
numsh=float(input("Number of Shares:\n"))
pp=float(input("Enter Purchase Price:\n"))
sp=float(input("Enter Selling Price:\n"))
comm=float(input("Enter Commission:\n"))
print("Stock Results",end='')
print("..................")
return num_stocks,name,numsh,pp,sp,comm
def calc(num_stocks,name,numsh,pp,sp,comm):
#calculations
for d in range (num_stocks):
Amtpaid=numsh*pp
bycomm=Amtpaid*comm
stocksold=numsh*sp
scomm=stocksold*comm
profit=(Amtpaid+comm)-(scomm-comm)
return Amtpaid,bycomm,stocksold,scomm,profit
def prnt(Amtpaid,bycomm,stocksold,scomm,profit):
#print outputs
print("Amount paid for stock:$",format(Amtpaid,',.2f'))
print("Commision paid on the purchase:$",format(bycomm,',.2f'))
print("Amount the stock sold for:$:",format(stocksold,',.2f'))
print("Commission paid on the sale:$",format(scomm,',.2f'))
print("Profit:$",format(profit,',.2f'))
print("\n")
main()
答案 0 :(得分:0)
在你的for循环中:
for d in range(num_stucks):
#todo
prnt(a, b, c, d, e, f)
因为当你运行循环一次时,你会返回值,所以它会打破循环。
答案 1 :(得分:0)
问题在于您在return
函数的for
循环中应用的load
语句。
将它带到循环之外。如果要调用其他函数,请在循环内调用它们。比如在你的情况下调用call
函数(你在main
中调用)而不是return语句
def main():
num_stocks,name,numsh,pp,sp,comm=load()
def load():
#load inputs
num_stocks=int(input("How many stocks do you want processed?"))
for i in range (num_stocks):
print("Stock number",i+1)
print(".....................")
name=input("Enter stock name:\n")
numsh=float(input("Number of Shares:\n"))
pp=float(input("Enter Purchase Price:\n"))
sp=float(input("Enter Selling Price:\n"))
comm=float(input("Enter Commission:\n"))
print("Stock Results",end='')
print("..................")
calc(num_stocks,name,numsh,pp,sp,comm)
def calc(num_stocks,name,numsh,pp,sp,comm):
#calculations
for d in range (num_stocks):
Amtpaid=numsh*pp
bycomm=Amtpaid*comm
stocksold=numsh*sp
scomm=stocksold*comm
profit=(Amtpaid+comm)-(scomm-comm)
prnt(Amtpaid,bycomm,stocksold,scomm,profit)
def prnt(Amtpaid,bycomm,stocksold,scomm,profit):
#print outputs
print("Amount paid for stock:$",format(Amtpaid,',.2f'))
print("Commision paid on the purchase:$",format(bycomm,',.2f'))
print("Amount the stock sold for:$:",format(stocksold,',.2f'))
print("Commission paid on the sale:$",format(scomm,',.2f'))
print("Profit:$",format(profit,',.2f'))
print("\n")
main()
这是解决方案。
答案 2 :(得分:0)
我在for
中只有一个main()
循环,它驱动了一系列调用你定义的函数以处理每个事务。恭喜你不使用全局变量,这可能是我看到新手所犯的头号错误。
我还强烈建议你开始学习(和跟随)PEP 8 - Style Guide for Python Code并使用更多的空格,这是你的朋友,因为它使你的代码更具可读性。
def main():
num_stocks = int(input("How many stocks do you want processed?"))
for stock_number in range(1, num_stocks+1):
name, numsh, pp, sp, comm = load(stock_number)
amtpaid, bycomm, stocksold, scomm, profit = calc(name, numsh, pp, sp,
comm)
prnt(amtpaid, bycomm, stocksold, scomm, profit)
def load(stock_number):
# load inputs for one transaction
print("Stock number", stock_number)
print(".....................")
name = input("Enter stock name:\n")
numsh = float(input("Number of Shares:\n"))
pp = float(input("Enter Purchase Price:\n"))
sp = float(input("Enter Selling Price:\n"))
comm = float(input("Enter Commission:\n"))
print("Stock Results", end='')
print("..................")
return name, numsh, pp, sp, comm
def calc(name, numsh, pp, sp, comm):
# calculations for one transaction
amtpaid = numsh*pp
bycomm = amtpaid*comm
stocksold = numsh*sp
scomm = stocksold*comm
profit = (amtpaid+comm) - (scomm-comm)
return amtpaid, bycomm, stocksold, scomm, profit
def prnt(amtpaid, bycomm, stocksold, scomm, profit):
# print output for one transaction
print("Amount paid for stock: ${:,.2f}".format(amtpaid))
print("Commision paid on the purchase: ${:,.2f}".format(bycomm))
print("Amount the stock sold for: ${:,.2f}".format(stocksold))
print("Commission paid on the sale: ${:,.2f}".format(scomm))
print("Profit: ${:,.2f}".format(profit))
print("\n")
main()