gpt4 book ai didi

python - 如何在没有 try/except 或 str.isdigit 的情况下检查字符串是否包含数字?

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:39 25 4
gpt4 key购买 nike

NUMBERS = "123456789"

def digit_checker(x):

for t in x:
if t in NUMBERS:
y = True
else:
y = False
return y



sentence = input("Enter a string to check if its all digits: ")
checker = digit_checker(sentence)
print(checker)

如标题所述,如果不使用 str.isdigittry/except,我将如何找到字符串是否包含所有数字。代码不断检查第一个字符而不是全部。我该如何解决?

最佳答案

NUMBERS = "123456789"

def digit_checker(x):
y = True
for t in x:
if t not in NUMBERS:
y = False
return y

关于python - 如何在没有 try/except 或 str.isdigit 的情况下检查字符串是否包含数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26748525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com