python - 即使输入正确也会出现错误消息。 Python

标签 python python-3.x input

我正在尝试使用此代码允许某些输入。

Sdepth = int(input("enter depth of slab: "))
if Sdepth != 45 or Sdepth != 38 :
    print("depth can only be 45 or 38")
    Sdepth = int(input("enter depth of slab: "))

如果我输入 45 或 38 print("深度只能是 45 或 38") 会输出,但不应该输出。

最佳答案

您需要and ,不是or在这种情况下。例如,如果您输入 45 , Sdepth != 38仍然是True ,因此整个 if 条件为 True 。或者您可以使用 if Sdepth not in (45, 38):

关于python - 即使输入正确也会出现错误消息。 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58197948/

相关文章:

C++ 循环 - 流错误?

python - Python 管道模块将如何执行 'cat'

python - 仅匹配 Python re 中的 unicode 字母

python - 分析python项目导入

python - pandas 从两个文件创建数据框

python - 用于解压缩带有大括号抑制的列表的 f-string 语法

css - 如何更改禁用输入的字体颜色?

c - 将文件中的内容添加到链表

python - 使用前置和后置方法调用装饰请求模块调用

python-3.x - Python 3 urllib urlretrieve 重试下载失败