我是一个完全的初学者,并且一直在查看 http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements 但我无法理解这里的问题.这很简单,如果用户输入 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
和不正确的缩进,它也不会像您预期的那样工作.要根据一组字符串检查一个字符串,请使用 in
.以下是你的做法(注意 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中字符串的if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!