简体   繁体   English

Python中的函数和While循环并发症

[英]Functions And While Loop Complication In Python

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

The objective of the first function is to allow a user to input the followings as many times as she or he wants until the user decides is done: 第一个功能的目的是允许用户根据自己的意愿多次输入以下内容,直到用户决定完成为止:

  1. Stock name 股票名称
  2. Shares bought 购买的股票
  3. Selling price 售价
  4. Broker commission 经纪人佣金

My main problem is then in the main function. 然后我的主要问题是在主要职能。 I am skeptical whether I am using the while loop correctly or if it's correct at all. 我对是否正确使用while循环还是完全正确表示怀疑。 I tried to run the program but it won't output anything. 我试图运行该程序,但不会输出任何内容。

Also, shouldn't I add this at the end of the program with the values inputted to call all the functions above: 另外,我不应该在程序的末尾添加输入值以调用上述所有函数的方法:

def main()
   load()
   calc()
   output()

Or is it fine within the while loop? 还是在while循环内很好?

I think a while loop is perfectly appropriate for this use case, where you want to loop an indeterminate number of times, stopping when some condition is not met. 我认为while循环非常适合这种用例,在这种情况下,您想循环无限次,在不满足某些条件时停止。

There is one obvious problem, on this line: 在这条线上有一个明显的问题:

stockname +=1

This doesn't make any sense. 这没有任何意义。 Since stockname is a string, you can't add one to it. 由于stockname是一个字符串,因此不能在其中添加一个。 Instead, you should be asking the user for the next stock name (or a "special" value to signal they're done). 相反,您应该向用户询问下一个股票名称(或“特殊”值以表示已完成)。 Try replacing that line with something like: 尝试用以下方式替换该行:

stockname = input("Enter the name of the next stock (or -999 to quit): ")

The rest of your code appears correct, if rather verbose. 您的其余代码看起来很正确,即使很冗长。 Unless you think it's likely you'll call some of your other functions in some other place in your code, it might be simpler and cleaner to include all the logic in one function. 除非您认为有可能在代码的其他位置调用其他函数,否则将所有逻辑包含在一个函数中可能更简单,更简洁。 Functions are nice, but you should balance the benefits of isolating each part of your code in its own function against the effort of passing lots of values between them. 函数是不错的选择,但是您应该在将代码的每个部分隔离到其自身的功能与在它们之间传递大量值的努力之间取得平衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM