我昨天刚开始学习Python,我正在try 编写的程序之一就是计算器.这段代码就是我遇到问题的地方.While循环应该在用户输入任何数学运算符时停止,但它仍然要求输入,即使用户输入了正确的字符.

我需要做些什么才能解决这个问题.

    op = None                                 
    while (op != "-" or op != "+" or op != "*" or op != "/"):
        op = input("Enter operator (-, +, *, /):  ")
    print(op)

推荐答案

  op = None                                 
while op not in ["-","+","*","/"]:
    op = input("Enter operat或 (-, +, *, /):  ")
print(op)

op = None                                 
while (op != "-" and op != "+" and op != "*" and op != "/"):
    op = input("Enter operat或 (-, +, *, /):  ")
print(op)

your code isnt w或king because while "op" might be equal to one of your options, its not equal to all the others, and theref或e continues the loop

Python相关问答推荐

ModuleNotFound错误:没有名为Crypto Windows 11、Python 3.11.6的模块

Pytest两个具有无限循环和await命令的Deliverc函数

"使用odbc_connect(raw)连接字符串登录失败;可用于pyodbc"

pyscript中的压痕问题

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

基于索引值的Pandas DataFrame条件填充

我对我应该做什么以及我如何做感到困惑'

我如何根据前一个连续数字改变一串数字?

如何在图中标记平均点?

无法连接到Keycloat服务器

基于形状而非距离的两个numpy数组相似性

为什么if2/if3会提供两种不同的输出?

在极中解析带有数字和SI前缀的字符串

在pandas/python中计数嵌套类别

用fft计算指数复和代替求和来模拟衍射?

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

正在try 让Python读取特定的CSV文件

将相应的值从第2列合并到第1列(Pandas )

使用_in链接操作管道传输的中间结果是否可用于链中的后续函数?

我应该使用哪一个来判断python中枚举值的唯一性?