我的节目有问题,我正在做一些功课。我的程序中有多个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
,它将重新打印之前相同的数字。
答案 0 :(得分:2)
将p
,b
,c
和s
的值设置为'y'
。
答案 1 :(得分:0)
此问题的解决方法很简单:在第一个p
的开头重置b
,s
,c
和while
的值循环:
repeat = 'y'
while repeat != 'n':
p = 'y'
b = 'y'
s = 'y'
c = 'y'
#Rest of code here
答案 2 :(得分:0)
您需要在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'
最重要的是你应该修复你的缩进,因为它似乎有点偏离。
答案 3 :(得分:0)
你需要写下这些行:
p = 'y'
b = 'y'
s = 'y'
在主while
循环内。然后它会解决你的问题。
答案 4 :(得分:0)
您需要重置第一个while循环内的条件。
那是
p = 'y'
b = 'y'
s = 'y'
在你的最后一行之后
答案 5 :(得分:0)
问题出在你的变量上。在第一个循环之后,变量 p,b,c 和 s 被赋值为'n'。因此,第二个循环将它们视为 n 否 y 你可以尝试类似的东西,
repeat = p = b = c = s = input("Would you like to go again y/n?: ").
可以有更好的方法。但问题出在第一个循环后的变量值。