在嵌套循环中返回2步

时间:2019-12-14 13:19:50

标签: python loops

def get_user_choice():
    global user_choice
    ''' 3users give their numbers to be validated'''
    for i in range(1, 4):

        choices = []
       ** choices = input("User {}, please enter 5 numbers separated by ',' :".format(i))
        nums = choices.split(',')

        while True:
            if len(nums) != 5:
                print(" Wrong choice,You have to enter 5 numbers separated by ','")
                break
            for nr in nums:
                if nr.isdigit():
                    if int(nr) < 1 or int(nr) > 25:
                        print(" Wrong! Enter 5 Numbers between 1 and 25")
                        continue
                else:
                    print(nr + " is not a number, try again")
                    continue
                     # How can i go back to ** from here????

1 个答案:

答案 0 :(得分:0)

此代码纠正了请求数字的逻辑,并返回选择的数字。

def get_user_choice():

    ''' 3users give their numbers to be validated'''
    #global user_choice--didn't understand why you needed a global user_choice so commented out for now

    user_choices = []
    for i in range(1, 4):

        while True:
            choices = input("User {}, please enter 5 numbers separated by ',' :".format(i))
            nums = choices.split(',')

            if len(nums) != 5:
                print(" Wrong choice,You have to enter 5 numbers separated by ','")
            else:
                for nr in nums:
                    if nr.isdigit():
                        if int(nr) < 1 or int(nr) > 25:
                            print(" Wrong! Enter 5 Numbers between 1 and 25")
                            break
                    else:
                        print(nr + " is not a number, try again")
                        break
                else:
                    user_choices.append(nums)
                    break

    return user_choices

# Get user choices                   
choices = get_user_choice()

上述代码的更干净的版本

def get_user_choice():
    ''' 3users give their numbers to be validated'''

    invalid_numbers = lambda numbers: list(filter(lambda y: not y.isdigit(), numbers))
    invalid_range = lambda numbers: list(filter(lambda y: not (1 <= int(y) <= 25), numbers))
    user_choices = []

    for i in range(1, 4):
      while True:
        choices = input("User {}, please enter 5 numbers separated by ',' :".format(i))
        nums = choices.split(',')

        if len(nums) != 5:
            print(" Wrong choice, You have to enter 5 numbers separated by ','")
            continue

        p = invalid_numbers(nums)

        if p:
            print(*p,' are not numbers.  Enter 5 numbers separaterd by "," ')
            continue

        p = invalid_range(nums)
        if p:
            print(*p, ' are out of range.  Enter 5 numbers between 1 and 25')   
            continue

        ## uncomment if you wanted the number rather than strings
        # nums = list(map(int, nums)) 
        user_choices.append(nums)
        break

    return user_choices

choices = get_user_choice()