【问题标题】:Functions And While Loop Complication In PythonPython中的函数和while循环复杂化
【发布时间】:2016-07-26 04:57:00
【问题描述】:
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 ()

第一个功能的目标是允许用户根据需要多次输入以下内容,直到用户决定完成:

  1. 股票名称
  2. 购买的股票
  3. 售价
  4. 经纪人佣金

我的主要问题是在 main 函数中。我怀疑我是否正确使用了while循环或者它是否正确。我试图运行该程序,但它不会输出任何内容。

另外,我不应该在程序末尾添加这个并输入值来调用上面的所有函数:

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

或者在while循环中可以吗?

【问题讨论】:

  • main() 添加到代码的最底部
  • 您在 while 条件中检查 stockname,但用户只能在 while 循环之前输入 。从来没有,所以一旦设置了stockname,它将永远保持这种状态。您可能想在 while 底部附近向用户询问新的stockname
  • 同样在第 18 行你使用 brokercommision 而你应该使用 brokercommission(缺少一个 s)
  • 在第 25 行,你使用 amountstockssoldfor,而你应该使用 amountstocksoldfor
  • 感谢您发现错误。更新。 @Racialz,我将主要内容移至底部。我应该将它与 while 循环一起移动吗?

标签: python function while-loop main


【解决方案1】:

我认为while 循环非常适合这种用例,在这种用例中,您希望循环不确定的次数,在不满足某些条件时停止。

这一行有一个明显的问题:

stockname +=1

这没有任何意义。因为stockname 是一个字符串,所以不能添加一个。相反,您应该询问用户下一个股票名称(或一个“特殊”值来表示他们已经完成)。尝试用以下内容替换该行:

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

您的代码的其余部分看起来是正确的,虽然比较冗长。除非您认为您可能会在代码中的其他位置调用某些其他函数,否则将所有逻辑包含在一个函数中可能会更简单、更清晰。函数很好,但您应该平衡将代码的每个部分隔离在自己的函数中的好处与在它们之间传递大量值的努力。

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 2016-07-23
    • 1970-01-01
    • 2022-09-22
    • 2012-07-03
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多