Python:如何将 input() 字符串与另一个字符串进行比较? [英] Python: how to compare input() string to another string?

查看:69
本文介绍了Python:如何将 input() 字符串与另一个字符串进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

x = input("print data? (Y/N) ")

while (x != 'Y' or x != 'N'):
    x = input("error: wrong input. Please put Y or N only ")

if x == 'Y':
    read_serial()

尝试检查键盘输入 (x) 是否等于 'Y' 或 'N' 字符串.如果不是,则循环继续直到它是.然而,上面的代码似乎编译和运行良好,除了不管循环继续运行什么.对 Python 3 还没有太多经验,所以有人能告诉我我做错了什么吗?

Trying to check whether or not the keyboard input (x) is equal to a 'Y' or 'N' string. And if not then the loop continues until it is. However the above code seems to compile and run fine except that no matter what the loop keeps running. Don't have much experience with Python 3 yet so could any one tell me what I'm doing wrong?

推荐答案

此声明

x != 'Y' or x != 'N'

总是True,因为世界上的一切都不是Y"或N".

is always True, because everything in the world is not "Y" or not "N".

改为:

x != 'Y' and x != 'N'

这篇关于Python:如何将 input() 字符串与另一个字符串进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆