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)
Hey there! I've been learning Python for 3-4 days and coded something like this3. My plan is taking the values from the list (CALCULATOR) and execute them as operations. However, I couldn't made the first while loop properly. How can I apply the while loop on both "operator" and "number" inputs?
Thank you so much.
Change while number or operator != "finish": to while number != "finish" and operator != "finish":.
while number or operator != "finish": first looks if bool(number) is True (this will be True when the string contains something), then looks if operator is unequal "finish", and then looks if one of the results is True.
You can break the while loop inside the loop anytime user provides finish.
If you use while number or operator != "finish": there is a problem
Since you are using
while number.isdigit() is False:
number = (input("Please insert a number"))
Use can't provide finish by that user can't get out of the loop. The user has to give a digit mandatorily.
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With