While循环中的While循环[Python]

2024-10-03 17:15:51 发布

您现在位置: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,它只会重新打印以前的数字。你知道吗

Example of the issue I am having


Tags: formatinputifnotfloatelsearevalues
3条回答

您需要在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'

最重要的是,你应该修复你的压痕,因为它似乎有点关闭。你知道吗

解决这个问题很简单:在第一个while循环开始时重置pbsc的值:

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

    #Rest of code here

pbcs的值设置为'y'。你知道吗

相关问题 更多 >