python - python中字符串的if语句?

标签 python conditional-statements

<分区>

我是个初学者,一直在看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()

最佳答案

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

一种方法:

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 - python中字符串的if语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6762959/

相关文章:

python - abspath 如何确定路径是否为相对路径?

javascript - Vuejs 三元运算符/条件在 v-bind-style 中不起作用

java - Java 中的贝叶斯网络实现

java - while 循环中的多个条件的条件顺序是否重要?

python - 如何在用 MatPlotLib 制作的散点图中为不同的点分配不同的颜色?

python - 如何从列表列表中复制一些元素,这样就不会影响复制列表中元素的值

python - 在 Amazon AWS 上的 PyPy 上运行 Django 应用程序

python - 有没有办法检查Python Socket中是否有传入消息

c# - 我如何重构此代码以不两次返回null?

java - 重复数字