2016-03-06 44 views
-1

我已经写了大部分的代码,但我仍然很难找出循环程序的代码(对于一个函数),直到用户完成为止。另外,我无法使用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

对于你想要的,你需要使用'while()'循环。条件应该是一些标志,当你从用户设置EOF时,循环也会停止。 –

+0

请指定您希望用户如何说他已完成。 –

+0

@AvihooMamka我会把这个while()放在def main()或其他地方吗? – Gangaji

回答

0

为了让用户决定是否要继续循环,而不是时间任何固定的号码,询问用户:

# in place of the call to main() above, put: 
while input('Proceed? ') == 'y': 
    main() 

所以它不断,只要用户输入“遍历main() Y”。根据需要,您可以将其更改为“是”,“是”等。

附注:
1.您应该使用多于1个空格进行缩进。通常4个空格,或至少2个。
2.阅读if __name__ == "__main__"

0

调用在一个循环的功能是相当简单的,你无论是在whilefor环包裹和内部调用它。下面的代码执行10次brookerage函数。我想你可以用这个作为例子,并根据你的需要定制整个事情。

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