繁体   English   中英

While循环中的While循环[Python]

[英]While Loop in a while loop [Python]

我的程序正在处理一些正在做的作业的问题。 我的程序中有多个while循环,第一个循环之后的循环似乎导致第一个循环只是重新打印用户输入的数据。

repeat = 'y'
p = 'y'
b = 'y'
s = 'y'
while repeat != 'n':
    while p == 'y':
            stocksPurchased = float(input("Number of stocks purchased: "))
            if stocksPurchased < 0:
                print("Negative Values are not allowed. Please re-enter.")
            else:
                    p = 'n'
    while b == 'y':                 
            pricePerStockBought = float(input("Amount per stock purchased in $: "))
            if pricePerStockBought < 0:
                print("Negative Values are not allowed. Please re-enter.")
            else:
                    b = 'n'
    while c == 'y':
            commissionWhole = float(input("Commission Rate as a percent %: "))
            if commissionWhole < 0:
                    print("Negative Values are not allowed. Please re-enter.")
            else:
                    c = 'n'

    while s == 'y':
            pricePerStockSold = float(input("Amount per stock sold in $: "))
            if pricePerStockSold < 0:
                    print("Negative Values are not allowed. Please re-enter.")
            else:
                    s = 'n'
    commissionRate = commissionWhole/100
    grossPurchasePrice = stocksPurchased*pricePerStockBought
    purchaseCommission = grossPurchasePrice*commissionRate
    totalPurchasePrice = grossPurchasePrice+purchaseCommission
    grossSalesPrice = stocksPurchased*pricePerStockSold
    saleCommission = grossSalesPrice*commissionRate
    netSalePrice = grossSalesPrice-saleCommission
    totalCommissionPaid = purchaseCommission+saleCommission
    profit = netSalePrice-totalPurchasePrice
    profitPercentage = (profit/grossPurchasePrice)*100

    print("Commission Fee paid after buying:  $", format(purchaseCommission,  ',.2f'))
    print("Amount stock sold for:             $", format(grossSalesPrice,     ',.2f'))
    print("Commission Fee paid after selling: $", format(saleCommission,      ',.2f'))
    print("Total Commission Paid:             $", format(totalCommissionPaid, ',.2f'))
    print("Total Profit made:                 $", format(profit,              ',.2f'))
    print("Profit Percentage:                 %", format(profitPercentage,    ',.1f'))

    if profitPercentage >= 8:
            print("Congrats! You beat the index fund!")
    elif 0 <= profitPercentage < 8:
            print("Well, you still made money")
    elif profitPercentage == 0:
            print("Nothing gained, nothing lost")
    else:
            print("Perhaps the stock market isn't for you")

                if totalCommissionPaid > profit:
                    print("Seems you should either pick different stocks, or find a cheaper broker")

    repeat = input("Would you like to go again y/n?: ")

如果我在此处输入y ,程序将重复执行,而不是重新提示数字,而只是重新打印前一次运行的数据。

例如,如果我分别输入数字: 1000, 10, 5, 15它将仅重印以前的相同数字。

我遇到的问题的例子

pbcs的值设置为'y'

解决此问题的方法很简单:在第一个while循环的开始处重置pbsc的值:

repeat = 'y'
while repeat != 'n':
    p = 'y'
    b = 'y'
    s = 'y'
    c = 'y'

    #Rest of code here

您需要在while循环中初始化变量以解决此问题。 这样,无论何时完成循环迭代,变量都将重新启动,因此满足下一个循环的条件。 因此,您的代码应为:

while repeat != 'n':
    repeat = 'y'
    p = 'y'
    b = 'y'
    s = 'y'
    c ='y'
    while p == 'y':
        stocksPurchased=float(input("Number of stocks purchased: "))
        if stocksPurchased < 0:
            print("Negative Values are not allowed. Please re-enter.")
        else:
            p = 'n'

最重要的是,您应该修复缩进,因为它似乎有点偏离。

您需要编写以下行:

p = 'y'
b = 'y'
s = 'y'

在主while循环内。 然后它将解决您的问题。

您需要在第一个while循环内重置条件。

那放

p = 'y'
b = 'y'
s = 'y'

最后一行之后

问题出在你的变量上。 在第一个循环之后,将变量p,b,cs分配为“ n”。 因此,第二个循环以他们为Ny您可以尝试像,

repeat = p = b = c = s = input("Would you like to go again y/n?: ").

可以有更好的方法。 但是问题出在第一个循环之后。

暂无
暂无

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

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