【问题标题】:unable to rule out number "0" from the user input in python using isdigit() method无法使用 isdigit() 方法从 python 中的用户输入中排除数字“0”
【发布时间】:2021-06-03 02:19:18
【问题描述】:

我必须在 python 中为用户创建一个列表,其中必须包含 0-10 之间的数字。应排除 '0' 和大于 9 的值。我已经使用“.isdigit()”编写了一个代码,以便消除单词,但由于控制台也在执行“0”,所以我被“0”消除所困扰。作为新手我尽力了。谁能帮帮我!?

def playerposition(): 
    p_keys= list(range(1,10))

    print('pick a position now: ')
    postn=input('choose a position between (1-9): ')
    
    while postn not in p_keys:
        while postn.isdigit()==False or postn==0:
            print('wrong input! enter again!: ')
            postn=input('choose a position between (1-9): ')
            
        print(int(postn))
        print(p_keys)
        break
playerposition()

执行的输出(控制台):

在(1-9)之间选择一个位置:你好 输入错误!再次输入!:

在 (1-9) 之间选择一个位置:1 1 [1、2、3、4、5、6、7、8、9]

运行单元 在 (1-9) 之间选择一个位置:0 0 [1, 2, 3, 4, 5, 6, 7, 8, 9] ---> 这是问题,其中 0 也被作为输入执行,而不是显示为 ('wrong input try again! : )

预期输出样本: 在 (1-9) 之间选择一个位置:0 输入错误!再次输入!: 在 (1-9) 之间选择一个位置:

【问题讨论】:

  • 您可以将post.isdigit()==False 简化为not post.isdigit()
  • input() 返回字符串和"0" != 0 这样就可以通过检查了。

标签: python output


【解决方案1】:

此行不正确:

 while postn.isdigit()==False or postn==0:

应该是

while postn.isdigit()==False or postn=="0":

【讨论】:

  • 感谢@jsumskas 的指出。刚刚更正了,谢谢。 #和平...!
【解决方案2】:

在您当前的代码中,会发生以下情况:

  1. p_keys 是整数列表,postninput() 返回的字符串。
  2. 字符串不会出现在整数列表中,因此 while postn not in p_keys: 始终为 True 并将 postn 传递给下一个 while 循环。
  3. while postn.isdigit() == False or postn == 0: 成功停止非数字字符串,但它无法停止具有值 "0"postn,因为字符串 "0" 不等于整数 0
  4. 将条件从postn == 0 转换为postn == "0" 确实排除了"0" 作为输入,但您仍然在这里遗漏了一些东西。所有像"46""82""100" 这样的数字字符串都可以通过检查,实际上"1" 在您的测试运行中通过了检查,因为它是数字而不是在p_keys 的范围内。

因此我改变了条件:

def playerposition():
    p_keys = list(range(1,10))

    print('pick a position now: ')
    postn = input('choose a position between (1-9): ')
    # 1. not postn.isdigit() stop non-numeric string so we can confirm postn is a numeric string
    # 2. we can safely convert postn to integer and check if it is in p_keys,
    #    it will stop 0 and other numbers as they are not in p_keys 
    while not postn.isdigit() or int(postn) not in p_keys:
        print('wrong input! enter again!: ')
        postn = input('choose a position between (1-9): ')

    print(int(postn))
    print(p_keys)

playerposition()

另一种方法是将p_keys 更改为字符串列表:

def playerposition():
    p_keys = [str(i) for i in range(1,10)]
    print('pick a position now: ')
    postn = input('choose a position between (1-9): ')
    # no need to check if postn is numeric or not, words not in p_keys can be ruled out already
    while postn not in p_keys:
        print('wrong input! enter again!: ')
        postn = input('choose a position between (1-9): ')

    print(int(postn))
    print(list(range(1,10)))

playerposition()

试运行:

pick a position now:
choose a position between (1-9): hello
wrong input! enter again!:
choose a position between (1-9): 10
wrong input! enter again!:
choose a position between (1-9): 0
wrong input! enter again!:
choose a position between (1-9): 1
1
[1, 2, 3, 4, 5, 6, 7, 8, 9]

【讨论】:

  • 非常感谢@adamkwm 。刚刚尝试使用您所显示的给定更正执行第一个代码,它执行得很好,我的错误被学习了,因为我是一个试图改进的新手。您的代码一切顺利
  • @Nithin 我也是一个想要提高的学习者,如果我能提供帮助,那就太好了。如果它解决了您的问题,您可以接受我的回答,或者如果需要,您可以询问更多信息。
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 2022-11-03
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多