【问题标题】:Python 2.7.6 while, and IfPython 2.7.6 while 和 If
【发布时间】:2014-03-27 03:22:10
【问题描述】:

我有一个简单的 python 脚本,其目标是在输入特定名称时打印额外的一行。这在 3.3.4 中有效,但是当我尝试在 2.7.6 中运行它时,额外的行不是输入名称时打印。我猜我在两个 python 版本之间的更改日志中查看了一些内容。非常感谢您的帮助。

while True:
    name =raw_input('Piggy: What is your name?\n').strip()
    if len(name.split()) == 1:
        name = name.title()
        break
    else:
        print("Ahhhj Make it shorter or actually enter a name. If you have a name like 'Mary Joe', please separate it by using a '-'")
if name ==('dawson'):
    print('Hey Developer.')

【问题讨论】:

  • raw_input 不在 Py 3.x 中,所以它不会起作用。
  • @thefourtheye 我在 python 2.7.6 中写这个,我不得不降级。

标签: python string python-2.7 methods python-3.3


【解决方案1】:

您调用name.title() 并替换name,然后将其与小写字符串进行比较。它永远不会匹配。

【讨论】:

  • 所以如果我在第二个 if 语句中将名称大写,因为名称被存储为标题,它会起作用吗?
  • 或者你可以比较name.lower()
猜你喜欢
  • 1970-01-01
  • 2011-08-08
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多