【问题标题】:How to keep asking user for input until I get an integer in a specific range?如何一直询问用户输入,直到我得到一个特定范围内的整数?
【发布时间】:2020-08-05 18:48:07
【问题描述】:

我最近决定重新审视我大约 3 个月前制作的井字游戏,并以全新的眼光进行调试,特别是这个错误一直困扰着我。基本上,我有这个代码:

def player_choice(board):
    '''Asks the player for their next position, calls a func to check if it's free'''
    '''and returns the position if it's free for later use'''
    spot = None
    while spot not in range(1, 10) or not space_check(board, spot):
        try:
            spot = int(input("Choose your next position (1-9): "))
        except:
            print("Hmm, looks to me like your input was invalid")
        else:
            break
    return spot

这是更大方案中的一个函数,但破坏整个游戏的是我需要一个 整数 作为输入,严格在 1 到 10 之间的范围内。在尝试错误处理之前,我使用了一个 while 循环,如果给出了一个 str,它会不断要求一个 int:

spot = int(input("Choose your next position (1-9): "))
    while spot not in range (1, 10) or not space_check(board, spot):
        spot = int(input("Looks like the spot you're trying to choose is invalid!\nPlease choose another position (1-9): "))
    return spot

但后来我切换到这个版本,这里它不会接受 str 作为输入,但它会接受 1-10 范围之外的 int。我的问题是:我该怎么做才能使这项工作按我需要的方式进行,严格取一个介于 1 和 10 之间的整数并继续询问直到准确地提供此输入?

【问题讨论】:

    标签: python python-3.x function error-handling while-loop


    【解决方案1】:

    移动一些东西,这似乎有效。虽然不知道为什么else总是被触发...

    def player_choice2(board):
        '''Asks the player for their next position, calls a func to check if it's free'''
        '''and returns the position if it's free for later use'''
        try:
            spot = int(input("Choose your next position (1-9): "))
        except:
            print("Hmm, looks to me like your input was invalid")
            spot = 0
        while spot not in range(1, 10) or not space_check(board, spot):
            try:
                spot = int(input("Choose your next position **  (1-9) **: "))
            except:
                print("Hmm, looks to me like your input was invalid")
            else:
                # This always fires...?
                print("Else....")
        return spot
    

    【讨论】:

    • 非常感谢!!这确实有效,我有点傻眼了,一点点调整如何让一切都到位。唯一的问题是现在我使用 Ctrl+C 时终端不会退出,哦好吧?
    • 哦,我发现 else 总是会触发,因为条件是输入一个 int,但是 45 或 95429 是一个 int,所以没有错误,但是循环的条件不是满意,因此错误不会触发,但循环将继续运行。它还解释了为什么如果我尝试输入任何 str 会出现错误。
    【解决方案2】:
    spot = " "
    while ord(spot) not in [49,58]:
        spot = input("Looks like the spot you're trying to choose is 
    invalid!\nPlease choose another position (1-9): ")
        try:
           spot = ord(spot)
        excpet:
           spot = " "
    return spot
    

    我在这里所做的是检查点的 ascii 值应该是数字,数字是 1->49、2->50 等等。

    【讨论】:

    • 好吧,不幸的是在尝试了这个之后,我可以说它不管什么原因都不起作用,并且不断重复打印我试图选择的位置是错误的......跨度>
    【解决方案3】:

    也许这会有所帮助:

        def player_choice(board):
            '''Asks the player for their next position, calls a func to check if it's free'''
            '''and returns the position if it's free for later use'''
            spot = True
            while spot in range(1, 10) or not space_check(board, spot):
                try:
                    spot = int(input("Choose your next position (1-9): "))
                except:
                    break
            else:
                print("Hmm, looks to me like your input was invalid")
        
            return spot
     
    

    【讨论】:

    • 不幸的是,它没有......这个循环允许一个非常超出范围的随机数,这正是我正在努力解决的问题
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多