I typed is digit function for user input, but when I enter an alphabet, it accepts the value

huangapple go评论64阅读模式
英文:

I typed is digit function for user input, but when I enter an alphabet, it accepts the value

问题

我创建了一个通过ID搜索客户姓名的代码。我使用isdigit函数来确保用户输入一个数字,但是当我输入一个字母时,它也接受了。为什么会这样?

英文:

            IDcheck = input("Enter The Customer ID ")
            if IDcheck.isdigit :
                print("Your ID is Being Searched")

            elif IDcheck.isalpha :
                print("IDs Only Contains Numbers")

I made a code to search customers names by ID . typed isdigit function to make sure the user enters a number
but when I type a letter, it does accept it. why

答案1

得分: 1

String.isdigit 是一个函数。

当解释器执行这一行代码 if IDcheck.isdigit: 时,它会检查是否存在一个名为 IDcheck.isdigit 的函数或变量。IDcheck.isdigit 存在,并返回 True。对于 IDcheck.isalpha 也是一样的情况。

要修复这个问题,将 IDcheck.isdigitIDcheck.isalpha 改为 IDcheck.isdigit()IDcheck.isalpha(),通过添加括号,如下所示:

IDcheck = input("Enter The Customer ID ")
if IDcheck.isdigit():
    print("Your ID is Being Searched")

elif IDcheck.isalpha():
    print("IDs Only Contains Numbers")

(Note: I have removed the HTML entities for quotation marks in the code for clarity.)

英文:

String.isdigit is a function.

When the interpreter executes the line if IDcheck.isdigit: it checks if a function or variable called IDcheck.isdigit exists. IDcheck.isdigit exist and it returns True. The same applies to IDcheck.isalpha.

To fix this: change IDcheck.isdigit and IDcheck.isalpha to IDcheck.isdigit() and IDcheck.isalpha() by adding parentheses, as in:

IDcheck = input("Enter The Customer ID ")
if IDcheck.isdigit():
    print("Your ID is Being Searched")

elif IDcheck.isalpha():
    print("IDs Only Contains Numbers")

huangapple
  • 本文由 发表于 2023年5月7日 11:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定