如果带有While循环的语句,试图使它从一个if到另一个,直到它到达结尾

时间:2016-05-15 23:52:31

标签: python python-3.x if-statement while-loop

我希望它能够如此,如果条件不再成立,它会继续在条件下移动,最终达到价格过高且顾客离开的状态。

yes_no = input("")
if yes_no == '2':
    trade = True
    while trade:
        if currentPrice < maxPrice - 40 and currentPrice < maxPrice - 16:
            priceIncrease = random.randint(8, 25)
            print(currentName,": ummm, sure i'll raise the price by ",priceIncrease)
            currentPrice += priceIncrease
            #this should always be the very last thing
            earnedMoney += currentPrice #

            trade = False

        elif currentPrice <= maxPrice - 16:                            #Change the '0.9' in future days to make it harder
            priceIncrease = random.randint(8, 25)
            print(currentName,": ughh, i'm not sure.... fine, i'll raise the price by",priceIncrease)

            #this should always be the very last thing
            earnedMoney += currentPrice #

            trade = False 

        elif currentPrice > maxPrice - 16 :
            print("Are you serious?? i know how much this product is worth \n")

            #this should always be the very last thing
            earnedMoney += currentPrice #
            trade = False

        else:
            print("this should never be printed")
            break

现在如果你运行程序,由于

,它只会继续下一个循环
trade = False

如果我删除它只是继续打印最初登陆的条件

请帮助这是一个学校项目,我似乎无法做到正确。

1 个答案:

答案 0 :(得分:0)

听起来你在说这种类型的循环:

trade = True
while trade:
    print "In the loop"
    trade = False
print "Past the loop"

永远不会打印"In the loop"。这是对的吗?

此外,设置一个可运行的示例会很有帮助,例如设置currentPricemaxPrice

例如,我认为这可以按预期工作:

trade = True
currentPrice = 10
maxPrice = 18
while trade:
    if currentPrice < maxPrice - 40 and currentPrice < maxPrice - 16:
        print "First branch"
        trade = False

    elif currentPrice <= maxPrice - 16:
        print "Second branch"
        trade = False

    elif currentPrice > maxPrice - 16 :
        print "Third branch"
        trade = False

    else:
        print("this should never be printed")
        break
print "After loop"

(另外,if的第一个分支有一些额外的逻辑...... currentPrice < maxPrice - 40currentPrice < maxPrice - 16并非真的都是必要的。)