问题描述
我是一个初学者,一直在查看,但我在这里无法理解问题。这很简单,如果用户输入y,则应该打印此内容,尽管我在IF answer == y
I am a total beginner and have been looking at http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements but I can not understand the problem here. It is pretty simple, if the user enters y it should print this will do the calculation although I get a syntax error on 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()
推荐答案
即使您修复了大小写错误的 if
和代码中的缩进,它也不会不能按您预期的那样工作。要对照一组字符串检查一个字符串,请在中使用
。这是您的操作方式(请注意,
if
都是小写,并且 if
块中的代码是
Even once you fixed the mis-cased if
and improper indentation in your code, it wouldn't work as you probably expected. To check a string against a set of strings, use in
. Here's how you'd do it (and note that if
is all lowercase and that the code within the if
block is indented one level).
一种方法:
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")
这篇关于如果在python中声明字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!