在不使用break的情况下突破python循环

时间:2013-11-04 17:54:22

标签: python

while answer  == 'Y':
    roll = get_a_roll()
    display_die(roll)
    if roll == first_roll:
        print("You lost!")
    amount_won = roll
    current_amount = amount_earned_this_roll + amount_won
    amount_earned_this_rol = current_amoun
    print("You won $",amount_won)
    print(  "You have $",current_amount)
    print("")
    answer = input("Do you want to go again? (y/n) ").upper()


if answer == 'N':
    print("You left with $",current_amount)
else:
    print("You left with $",current_amount)

使用此循环的目的是在游戏中,掷骰子,并且每个卷的数量奖励你,除非你滚动匹配你的第一卷。现在,我需要循环停止,如果发生这种情况,我知道这很容易使用break语句实现,但是,我已经被告知不允许使用break语句。如果roll == first_roll?

,如何让循环终止?

6 个答案:

答案 0 :(得分:4)

你可以:

  • 使用标志变量;你已经在使用它了,只需在这里重复使用它:

    running = True
    while running:
        # ...
        if roll == first_roll:
            running = False
        else:
            # ...
            if answer.lower() in ('n', 'no'):
                running = False
            # ...
    
  • 从函数返回:

    def game():
        while True:
            # ...
            if roll == first_roll:
                return
            # ...
            if answer.lower() in ('n', 'no'):
                return
            # ...
    
  • 提出异常:

    class GameExit(Exception):
        pass
    
    try:
        while True:
            # ...
            if roll == first_roll:
                raise GameExit()
            # ...
            if answer.lower() in ('n', 'no'):
                raise GameExit()
            # ...
    except GameExit:
        # exited the loop
        pass
    

答案 1 :(得分:1)

如果要退出循环,可以使用要设置为false的变量。

cont = True
while cont:
    roll = ...
    if roll == first_roll:
        cont = False
    else:
        answer = input(...)
        cont = (answer == 'Y')

答案 2 :(得分:1)

获得一些奖励积分和注意力,使用生成器功能。

from random import randint

def get_a_roll():
    return randint(1, 13)

def roll_generator(previous_roll, current_roll):
    if previous_roll == current_roll:
        yield False
    yield True

previous_roll = None 
current_roll = get_a_roll()

while next(roll_generator(previous_roll, current_roll)):
    previous_roll = current_roll
    current_roll = get_a_roll()
    print('Previous roll: ' + str(previous_roll))
    print('Current roll: ' + str(current_roll))
print('Finished')

答案 3 :(得分:0)

是否允许continue?它可能与break太相似(两者都是受控goto的类型,其中continue返回到循环的顶部而不是退出它),但这是一种使用它的方法:

while answer  == 'Y':
    roll = get_a_roll()
    display_die(roll)
    if roll == first_roll:
        print("You lost!")
        answer = 'N'
        continue
    ...

如果丢失,answer被硬编码为“N”,因此当您返回顶部重新评估条件时,它将为false并且循环终止。

答案 4 :(得分:0)

npm install -g @angular/cli

答案 5 :(得分:0)

说明:你定义了一个 end_game 函数,它在最后做你想做的事情,然后结束代码

#do this 
def end_game()
    if answer == 'N':
        print("You left with $",current_amount)
    else:
        print("You left with $",current_amount)
        exit()
while answer  == 'Y':
    roll = get_a_roll()
    display_die(roll)
    if roll == first_roll:
        print("You lost!")
        end_game()
    amount_won = roll
    current_amount = amount_earned_this_roll + amount_won
    amount_earned_this_rol = current_amoun
    print("You won $",amount_won)
    print(  "You have $",current_amount)
    print("")
    answer = input("Do you want to go again? (y/n) ").upper()