python - 如何在 while 循环中连接两个值?

标签 python

print("Welcome to the CALCULATORBRO. Please type 'finish' when you have finished.")
CALCULATOR = []
number = ""
operator = ""
while number or operator != "finish":
    number = input("Please insert a number.")
    while number.isdigit() is False:
        number = input("Please insert a number")
        CALCULATOR.append(number)
        operator = input("Insert an operator.")
        OPERATORLIST = ["+", "-", "/", "*"]
        while operator not in OPERATORLIST:
            operator = input("Please insert an operator.")
            CALCULATOR.append(operator)

你好!我已经学习 Python 3-4 天了,并且编写了类似这样的代码3。我的计划是从列表 (CALCULATOR) 中获取值并将它们作为操作执行。但是,我无法正确地进行第一个 while 循环。如何在“运算符”和“数字”输入上应用 while 循环?

非常感谢。

最佳答案

while number or operator != "finish": 更改为 while number != "finish"and operator != "finish":
while number or operator != "finish": 首先查看 bool(number) 是否为 True(这将是 True 当字符串包含某些内容时),然后查看 operator 是否不等于 "finish",然后查看其中一个结果是否为 True。

关于python - 如何在 while 循环中连接两个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65863187/

相关文章:

python - 无法确定从 SWIG(不是 ctypes)C 传递给 python 例程的正确参数

python - 对包含 B 或 I 标签的连续单词进行分组

Python 毒物依赖于模式化命令

python - 如何以pythonic方式将字符串名称从列表中放入新字符串

python / Pandas : Calculation based on cell value

php - key 长度问题: AES encryption on phpseclib and decryption on PyCrypto

python - MongoDB 与 Python,用于存储游戏的用户名和密码

python - 使用 Python 识别 Mac OS X 中的包目录

python - 在 Python 中生成一个随机字母

python - Django 1.10 : extend/override admin css