Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect two values in while loop?

Tags:

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)

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.

like image 318
Deniz Kaya Avatar asked Nov 18 '25 23:11

Deniz Kaya


2 Answers

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.

like image 148
TheEagle Avatar answered Nov 21 '25 14:11

TheEagle


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)))
like image 34
Epsi95 Avatar answered Nov 21 '25 14:11

Epsi95