Redo for loop iteration in Python

前端 未结 6 1405
灰色年华
灰色年华 2020-12-10 04:07

Does Python have anything in the fashion of a \"redo\" statement that exists in some languages?

(The \"redo\" statement is a statement that (just like \"break\" or \

6条回答
  •  抹茶落季
    2020-12-10 04:16

    I just meet the same question when I study perl,and I find this page.

    follow the book of perl:

    my @words = qw(fred barney pebbles dino wilma betty);
    my $error = 0;
    
    my @words = qw(fred barney pebbles dino wilma betty);
    my $error = 0;
    
    foreach (@words){
        print "Type the word '$_':";
        chomp(my $try = );
        if ($try ne $_){
            print "Sorry - That's not right.\n\n";
            $error++;
            redo;
        }
    }
    

    and how to achieve it on Python ?? follow the code:

    tape_list=['a','b','c','d','e']
    
    def check_tape(origin_tape):
        errors=0
        while True:
            tape=raw_input("input %s:"%origin_tape)
            if tape == origin_tape:
                return errors
            else:
                print "your tape %s,you should tape %s"%(tape,origin_tape)
                errors += 1
                pass
    
    all_error=0
    for char in tape_list:
        all_error += check_tape(char)
    print "you input wrong time is:%s"%all_error
    

    Python has not the "redo" syntax,but we can make a 'while' loop in some function until get what we want when we iter the list.

提交回复
热议问题