A
A
Adolf452021-06-08 19:37:08
Python
Adolf45, 2021-06-08 19:37:08

Why is the code in the if block executed even if the condition is false?

I am writing my own calculator and I want the user to re-enter the data if the input is incorrect, but until the correct one is entered

what = input("Выберу операцию(+),(-),(*),(/):")

if what == "+" or "-" or "*" or "/":
    a = int(input("Первое число: "))
    b = int(input("Второе число: "))

    c = 0

    if what == "+":
        c = a + b
    elif what == "-": 
        c = a - b
    elif what == "*":
        c = a * b
    elif what == "/":
        c = a / b

    print(c)    
else:
    i = True
    while i == True:
        print("Неверная команда! Используй только '+','-','*','/':")
        what = input("Выберу операцию(+),(-),(*),(/):")
        if what == "+" or "-" or "*" or "/":
               i = False

But the problem is that if you enter an inappropriate value, then the code from the if block is still executed. I don't know why, I hope you can help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jordan_Belforts, 2021-06-08
@Jordan_Belforts

if what in ['+', '-', '*', '/']:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question