【问题标题】:While loop for a raw_input gives strange output in Python用于 raw_input 的循环在 Python 中给出了奇怪的输出
【发布时间】:2014-08-23 00:37:09
【问题描述】:

不管我给出什么输入,我得到的输出是“我认为你已经长大了去冒险。你的冒险到此结束。” 就像我输入 17 一样,我会得到“我认为你已经长大了去冒险。你的冒险到此结束。”如果我输入 18-59,我会得到“我认为你已经长大了去冒险。你的冒险到此结束。”

while True:
    age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
    if age.isdigit() == False:
        print "\n Please, put only in numbers!"
        time.sleep(3)
    elif age < 18:
        print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
        time.sleep(7)
        exit(0) 
    elif age >= 60:
        print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
        time.sleep(5)
        exit(0)
    else:
        print "\n %s. You're starting to get old." % age
        break

【问题讨论】:

  • raw_input 每次都会给您一个字符串,因此您无法将其与数字进行比较。试试int(raw_input(...))

标签: python python-2.7 input while-loop


【解决方案1】:

您需要将您的输入作为int进行比较

age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))

否则,您会将strint 进行比较。看下面的例子

>>> 5 < 10
True
>>> type(5)
<type 'int'>

>>> '5' < 10
False
>>> type('5')
<type 'str'>

在 Python 2.x 中,比较不同类型的值通常会忽略这些值,而是比较类型。因为str &gt;= int,任何字符串都是&gt;= 任何整数。在 Python 3.x 中,您会得到一个 TypeError,而不是默默地做一些令人困惑且难以调试的事情。

【讨论】:

  • 一个问题:他正在检查age.isdigit,所以现在将其转换为int 还为时过早。您要么需要将转换移动到第一个 if 下的 else 子句中,要么使用 try/except 而不是 if,或者以其他方式重组事物。
  • 这给了我一个 'int' 对象没有属性 'isdigit' 是不是因为 'isdigit' 只适用于字符串?
  • 正确。例如,如果您尝试将str 转换为包含字母的int,您将得到ValueError。因此,您可以在尝试投射之前先检查数字。
  • 在这种情况下,请尝试在 elif int(age) &lt; 18elif int(age) &gt;= 60 上进行测试。
  • 这仍然给我'int'对象没有属性'isdigit'
【解决方案2】:

问题在于raw_input 总是返回一个字符串对象,而这些对象并不能真正与int 类型进行比较。你需要做一些类型转换。

如果您想使用isdigit 来测试输入是否为数字,那么您应该这样进行:

while True:
    age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
    if age.isdigit() == False:
        print "\n Please, put only in numbers!"
        time.sleep(3)
    elif int(age) < 18:
        print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
        time.sleep(7)
        exit(0) 
    elif int(age) >= 60:
        print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
        time.sleep(5)
        exit(0)
    else:
        print "\n %s. You're starting to get old." % age
        break

但是,您可以通过立即转换为整数来稍微简化代码,如果它是无效字符串则捕获异常:

while True:
    try:
        age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
        if age < 18:
            print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
            time.sleep(7)
            exit(0) 
        elif age >= 60:
            print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
            time.sleep(5)
            exit(0)
        else:
            print "\n %s. You're starting to get old." % age
            break
    except ValueError:
        print "\n Please, put only in numbers!"
        time.sleep(3)

【讨论】:

  • 谢谢,在 tryexcept 例外情况下完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2013-06-25
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
相关资源
最近更新 更多