我已经编写了大部分代码,但是我仍然很难找到用于循环程序(用于函数)的代码,直到用户完成为止。此外,我无法使用For循环。
def load():
a=input("Name of the stock: ")
b=int(input("Number of shares Joe bought: "))
c=float(input("Stock purchase price: $"))
d=float(input("Stock selling price: $"))
e=float(input("Broker commission: "))
return a,b,c,d,e
def calc(b,c,d,e):
w=b*c
x=c*(e/100)
y=b*d
z=d*(e/100)
pl=(x+z)-(y-z)
return w,x,y,z,pl
def output(a,w,x,y,z,pl):
print("The Name of the Stock: ",a)
print("The amount of money Joe paid for the stock: $",format(w,'.2f'))
print("The amount of commission Joe paid his broker when he bought the stock: $",format(x,'.2f'))
print("The amount that Jim sold the stock for: $",format(y,'.2f'))
print("The amount of commission Joe paid his broker when he sold the stock: $",format(z,'.2f'))
print("The amount of money made or lost: $",format(pl,'.2f'))
def main():
a,b,c,d,e=load()
w,x,y,z,pl=calc(b,c,d,e)
output(a,w,x,y,z,pl)
main()
答案 0 :(得分:0)
在循环中调用函数非常简单,您可以在while
或for
循环中包装并在其中调用它。以下代码执行brookerage
函数10次。我想你可以用它作为例子,并根据你的需要定制整个事物。
def brookerage():
a,b,c,d,e=load()
w,x,y,z,pl=calc(b,c,d,e)
output(a,w,x,y,z,pl)
def main():
for i in range(0,10):
brookerage()
main()
答案 1 :(得分:0)
要让用户决定是否要保持循环而不是任何固定次数,请询问用户:
# in place of the call to main() above, put:
while input('Proceed? ') == 'y':
main()
只要用户输入“y”,它就会一直循环main()
。您可以根据需要将其更改为“是”,“是”等。
旁注:
1.你应该使用多于1个空格进行缩进。通常为4个空格,或至少2个
2.阅读if __name__ == "__main__"
。