英文:
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.isdigit
和 IDcheck.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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论