gpt4 book ai didi

python - 为什么 `x != x.isdigit()` 不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:50 24 4
gpt4 key购买 nike

我需要制作一个循环,提示用户输入有效的数字字符串,并且必须询问用户,直到他们输入正确的输入。

我认为我的想法是正确的,但我不完全确定如何更正错误的输入。

def c():
x = input("Enter a String of Digits")
while x != x.isdigit()
i = input("enter correct data string")
else:
print("True")

c()

最佳答案

str.isdigit() 返回一个 bool 值(True/False),不要将它与x 本身进行比较,而只是使用返回值:

def c():
x = input("Enter a String of Digits")
while not x.isdigit():
x = input("enter correct data string")
print("True")

c()
  • 已修复语法错误:在 while ..: 行中添加缺少的 :
  • i = ... 更改为 x = ...

关于python - 为什么 `x != x.isdigit()` 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34100616/

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