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天了,并编写了类似this3的代码。我的计划是将列表中的值(计算器)作为操作执行。但是,我无法正确地完成第一个while循环。如何对“运算符”和“数字”输入应用while循环?
非常感谢。
发布于 2021-01-23 19:04:36
将while number or operator != "finish":更改为while number != "finish" and operator != "finish":。
while number or operator != "finish":首先查看bool(number)是否为True (当字符串包含某些内容时,这将是True ),然后查看operator是否为不相等的"finish",然后查看结果之一是否为True。
发布于 2021-01-23 19:10:16
只要用户提供break,您就可以在循环中使用while循环。
如果您使用while number or operator != "finish":,则会出现一个问题,因为您正在使用
while number.isdigit() is False:
number = (input("Please insert a number"))Use不能提供finish,因为用户无法脱离循环。用户必须强制提供一个数字。
print("Welcome to the CALCULATORBRO. Please type 'finish' when you have finished.")
CALCULATOR = []
number = ""
operator = ""
while True:
number = input("Please insert a number 1: ")
if (number == 'finish'): # break the loop and come out
break
while number.isdigit() is False:
number = (input("Please insert a number2: "))
if (number == 'finish'): # break the loop and come out
break
CALCULATOR.append(number)
operator = input("Insert an operator.")
if (operator == 'finish'):# break the loop and come out
break
OPERATORLIST = ["+", "-", "/", "*"]
while operator not in OPERATORLIST:
operator = input("Please insert an operator.")
if (operator == 'finish'): # break the loop and come out
break
CALCULATOR.append(operator)
number = input("Please insert a number 2: ")
if (number == 'finish'):# break the loop and come out
break
while number.isdigit() is False:
number = (input("Please insert a number 2: "))
if (number == 'finish'): # break the loop and come out
break
CALCULATOR.append(number)
print('result',eval("".join(CALCULATOR)))https://stackoverflow.com/questions/65863187
复制相似问题