【问题标题】:If statement for strings in python? [duplicate]python中字符串的if语句? [复制]
【发布时间】:2011-10-09 10:18:19
【问题描述】:

我是一个初学者,一直在看http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements,但我无法理解这里的问题。这很简单,如果用户输入 y 它应该打印这将进行计算,尽管我在 IF answer=="y"

上收到语法错误
answer = str(input("Is the information correct? Enter Y for yes or N for no"))
proceed="y" or "Y" 
If answer==proceed:
print("this will do the calculation"):
else:
exit()

【问题讨论】:

  • 下面的答案应该对您有所帮助。但看起来您可能需要从头开始并遵循编程教程。最好在潜入之前学习基础知识!

标签: python conditional-statements


【解决方案1】:

If 应该是if。您的程序应如下所示:

answer = raw_input("Is the information correct? Enter Y for yes or N for no")
if answer.upper() == 'Y':
    print("this will do the calculation")
else:
    exit()

还要注意缩进很重要,因为它在 Python 中标记了一个块。

【讨论】:

  • 还是错了。 raw_input(),不是str(input())
  • @agf:你是对的,解决了这个问题。
  • 我必须在打印后取出冒号(“这将进行计算”)
  • @Mr.Tea 这确实是一个错误,我已经解决了,谢谢!
  • @BjörnPollex 你可能已经七年没看到你的答案了!!!哈哈哈仍然被使用:)
【解决方案2】:

你想要:

answer = str(raw_input("Is the information correct? Enter Y for yes or N for no"))
if answer == "y" or answer == "Y":
  print("this will do the calculation")
else:
  exit()

或者

answer = str(raw_input("Is the information correct? Enter Y for yes or N for no"))
if answer in ["y","Y"]:
  print("this will do the calculation")
else:
  exit()

注意:

  1. 这是“如果”,而不是“如果”。 Python 区分大小写。
  2. 缩进很重要。
  3. python 命令末尾没有冒号或分号。
  4. 你想要raw_input而不是inputinput 评估输入。
  5. “或”如果计算结果为真,则为您提供第一个结果,否则为您提供第二个结果。所以"a" or "b" 计算为"a",而0 or "b" 计算为"b"。见The Peculiar Nature of and and or

【讨论】:

  • 你的第五点不正确。 x or y 如果计算结果为 true,将返回第一个值,否则返回第二个值。
【解决方案3】:

Python 区分大小写,需要适当的缩进。您需要使用小写的“if”,正确缩进您的条件并且代码有错误。 proceed 将评估为 y

【讨论】:

  • proceed 不会计算为 true,它将计算为 'y'
【解决方案4】:

即使您在代码中修复了错误大小写的 if 和不正确的缩进,它也不会像您预期的那样工作。要根据一组字符串检查一个字符串,请使用in。以下是你的做法(注意if 全部小写,if 块内的代码缩进一级)。

一种方法:

if answer in ['y', 'Y', 'yes', 'Yes', 'YES']:
    print("this will do the calculation")

另一个:

if answer.lower() in ['y', 'yes']:
    print("this will do the calculation")

【讨论】:

  • 另请注意,在 OP 发布的代码中,缩进是错误的。
  • 你漏掉了str(input())不起作用的问题,raw_input()是正确的。
  • @agf 这取决于他使用的 Python 版本,但如果它可以工作(即使用 Python 3.x),那将是多余的。
  • 很高兴知道——我错过了这个变化。谢谢。
【解决方案5】:
proceed = "y", "Y"
if answer in proceed:

你也不想

answer = str(input("Is the information correct? Enter Y for yes or N for no"))

你想要的

answer = raw_input("Is the information correct? Enter Y for yes or N for no")

input() 计算输入为 Python 表达式的任何内容,raw_input() 返回一个字符串。

编辑:这仅在 Python 2 上是正确的。在 Python 3 上,input 很好,尽管 str() 包装仍然是多余的。

【讨论】:

  • "input() 计算作为 Python 表达式输入的任何内容,raw_input() 返回一个字符串。"除了在 Python 3 中,input() 执行 raw_input() 以前的工作,如果您希望输入被评估为 Python 表达式,则可以使用 eval(input()) 或其他东西。
【解决方案6】:

Python 是一种区分大小写的语言。所有 Python 关键字都是小写的。使用if,而不是If

另外,在调用print() 之后不要加冒号。此外,缩进 print()exit() 调用,因为 Python 使用缩进而不是括号来表示代码块。

而且,proceed = "y" or "Y" 不会做你想做的事。使用proceed = "y"if answer.lower() == proceed:,或类似的东西。

还有一个事实是,只要输入值不是单个字符“y”或“Y”,您的程序就会退出,这与替代情况下的“N”提示相矛盾。代替你的else 子句,使用elif answer.lower() == info_incorrect:,事先在某处使用info_incorrect = "n"。如果输入值是其他值,则只需重新提示响应或其他内容。


如果您现在的学习方式遇到这么多麻烦,我建议您阅读 Python 文档中的教程。 http://docs.python.org/tutorial/index.html

【讨论】:

  • @samb: or 呢?因为"y" 的布尔值为True,所以proceed = "y" or "Y" 的结果将只是将"y" 赋值给proceed,不考虑大写。不过,Daniel 对 in 运算符的使用很好。
  • 对不起,我在你编辑你的帖子之前添加了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 2018-03-09
相关资源
最近更新 更多