Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

python - How to connect two values in while loop?

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.

question from:https://stackoverflow.com/questions/65863187/how-to-connect-two-values-in-while-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...