如何判断输入是否为字母?

发布于 2024-09-28 06:07:32 字数 447 浏览 6 评论 0原文

我已经尝试解决这个问题有一段时间了,但似乎无法使其正常工作..这是我当前的工作

while True:

    guess = int(raw_input('What is your number?'))

    if 100 < guess or guess < 1:
        print '\ninvalid'

    else:
        .....continue on

现在我已经做到了,因此当用户输入高于 100 或低于 1 的数字时,它会打印出“无效”。但是,如果我想让它当用户输入一个不是数字的字符串(字母、标点符号等)时它也返回这个“无效”消息怎么办?

我曾考虑过使用 if not ...isdigit() ,但它不起作用,因为我将猜测作为整数以便上述范围起作用。 Try/ except 是我想到的另一个选择,但仍然没有弄清楚如何正确实现它。

I been trying to solve this one for a while and can't seem to make it work right.. here is my current work

while True:

    guess = int(raw_input('What is your number?'))

    if 100 < guess or guess < 1:
        print '\ninvalid'

    else:
        .....continue on

Right now I have made it so when a user input a number higher than 100 or lower than 1, it prints out "invalid". BUT what if i want to make it so when a user input a string that is not a number(alphabetic, punctuation, etc.) it also returns this "invalid" message?

I have thought about using if not ...isdigit(), but it won't work since I get the guess as an integer in order for the above range to work. Try/except is another option I thought about, but still haven't figured out how to implement it in correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-10-05 06:07:32

您可以使用异常处理:

try:
    guess = int(raw_input('What is your number?'))
    if not (1 <= guess <= 100):
        raise ValueError
    # .....continue on
except ValueError:
    print '\ninvalid'

这样,如果用户输入非数字字符串或输入大于 100 或小于 1 的数字字符串,则将打印 \ninvalid

编辑:< /strong> 好的,我提交给 x < y < z 语法。不过,仍然认为当它与 not 一起使用时,它会失去一些魅力。

You can use exception handling:

try:
    guess = int(raw_input('What is your number?'))
    if not (1 <= guess <= 100):
        raise ValueError
    # .....continue on
except ValueError:
    print '\ninvalid'

That way, \ninvalid will be printed if the user either inputs a non-numeric string or inputs a numeric string greater than 100 or smaller than 1.

EDIT: Okay, I submit to the x < y < z syntax. Still think it loses some of its charm when it's used with not, though.

花辞树 2024-10-05 06:07:32
while True:
  try:
    guess = int(raw_input("..."))
  except EOFError:
    print "whoa nelly! EOF? we should probably exit"
    break  # or sys.exit, or raise a different exception,
    # or don't catch this at all, and let it percolate up,
    # depending on what you want
  except ValueError:
    print "illegal input: expected an integer"
  else:
    if not (1 <= guess <= 100):
      print "out of range"
    else:
      print "processing guess... (but if it wasn't 42, then it's wrong)"
      break  # out of while loop after processing
while True:
  try:
    guess = int(raw_input("..."))
  except EOFError:
    print "whoa nelly! EOF? we should probably exit"
    break  # or sys.exit, or raise a different exception,
    # or don't catch this at all, and let it percolate up,
    # depending on what you want
  except ValueError:
    print "illegal input: expected an integer"
  else:
    if not (1 <= guess <= 100):
      print "out of range"
    else:
      print "processing guess... (but if it wasn't 42, then it's wrong)"
      break  # out of while loop after processing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文