def main():
totalprofit = 0
stockname = input("Enter the name of the stock or -999 to quit: ")
while stockname != "-999":
sharesbought, purchasingprice, sellingprice, brokercommission = load()
amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss = calc(sharesbought, purchasingprice, sellingprice, brokercommission)
output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofpaidcommission, profitorloss)
stockname = input("Enter the name of the next stock (or -999 to quit): ")
totalprofit += profitorloss
print("\n Total profit is: ", format(totalprofit, '.2f'))
def load():
sharesbought = int(input("Number of shares bought: "))
purchasingprice = float(input("Purchasing price: "))
sellingprice = float(input("Selling price: "))
brokercommission = float(input("Broker commission: "))
return sharesbought, purchasingprice, sellingprice, brokercommission
def calc(sharesbought, purchasingprice, sellingprice, brokercommission):
amountpaid = sharesbought * purchasingprice
amountofpaidcommission = amountpaid * (brokercommission/100)
amountstocksoldfor = sharesbought * sellingprice
amountofsoldcommission = amountstocksoldfor * (brokercommission/100)
profitorloss = (amountpaid + amountofpaidcommission) - (amountstocksoldfor - amountofsoldcommission)
return amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss
def output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss,):
print("\n Stock name: ", stockname, sep = '')
print("Amount paid for the stock: ", format(amountpaid, '.2f'))
print("Commission paid to broker when the stock was bought: ", format(amountofpaidcommission, '.2f'))
print("Amount the stock sold for: ", format(amountstocksoldfor, '.2f'))
print("Commission paid to broker when the stock was sold: ", format(amountofsoldcommission, '.2f'))
print("Profit or loss: ", format(profitorloss, '.2f'))
main()
birinci fonksiyonun objektif o veya onun istediği gibi kullanıcı yapılır karar verene kadar girişine bir kullanıcıya birçok kez de yakından takip izin vermektir
iken:Fonksiyonlar Ve Döngü komplikasyon olarak Python
- Stok isim
- Paylar
- Satış fiyatı
- Broker komisyonu
Ana problemim ana işlevde. While döngüsünü doğru kullanıp kullanmadığımı ya da doğru olup olmadığını belirsizim. Programı çalıştırmayı denedim ama hiçbir şey çıkarmayacak.
def main()
load()
calc()
output()
Yoksa süre döngü içinde gayet: değerlerinin yukarıdaki tüm işlevleri çağırmak için girilen ile aynı
, ben program sonunda bu eklemek gerekmez mi?
'main()' kodunu kodunuzun en altına ekleyin – Keatinge
while koşulunda "stockname" değerini denetleyin, ancak kullanıcı yalnızca while * while döngüsünü girebilir. Asla sonra, bir kez 'stockname' ayarlandığında, sonsuza dek bu şekilde kalacaktır. Kullanıcıya yeni bir 'stockname' için zamanın altından yaklaşmasını isteyebilirsiniz. – Evert
da 18 satırında 'brokercommision' kullandığınızda, 'brokercommision' kullanırken (0) eksik olan – Keatinge