【问题标题】:Going back 2 step in nested loops在嵌套循环中返回 2 步
【发布时间】:2019-12-14 13:19:50
【问题描述】:
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????

【问题讨论】:

  • 与其使用continue,不如使用break。它应该跳出内循环,回到外循环。
  • 点评来源: 请在您的帖子中添加描述和问题。见:HOW TO ASK

标签: python loops


【解决方案1】:

此代码更正了请求号码的逻辑并返回所选号码。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多