Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use of "and" in python and solution to this error

The complete python code for a simple calculator is –

print("The program is calculator that can operate on 2 numbers")

num1 = input("Enter first number: ")
num2 = input("Enter second number: ")

try:
    num1 = float(num1)
except ValueError:
    print("first number is not a valid number")

try:
    num2 = float(num2)
except ValueError:
    print("second number is not a valid number")

print('''choose operation from 
*(multiply)
/(divide)
+(add)
-(subtract)
%(find remainder)''')

operator = input("Operation:")

if(operator != "*" and "/" and "+" and "-" and "%"): {
       print("please enter a valid operation")
}

if (operator == "*"): {
    print(num1 * num2)
}

if (operator == "/"):{
    print(num1 / num2)
}

if (operator == "+"): {
    print(num1 + num2)
}

if (operator == "-"): {
    print(num1 - num2)
}

if (operator == "%"): {
    print(num1 % num2)
}

I was building this program as simple calculator but the problem is in –

print('''choose operation from 
*(multiply)
/(divide)
+(add)
-(subtract)
%(find remainder)''')

operator = input("Operation:")

if(operator != "*" and "/" and "+" and "-" and "%"): {
       print("please enter a valid operation")
}

Here, even if I enter a valid operation it shows the error, please enter a valid operation [which I intended to work only when people enter like "3", "lol" in operation field]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My understanding or and operation is all the things should be true for Eg. –

12 == 12 and 13 and 14

will be false as it might be equal to 12 but not to 13 and 14
Please help me fix this error and understand logical operators (and , not , or(all of them))

>Solution :

The logical operators doesn’t work this way. First thing, you cannot use and to give multiple strings to compare, every time you need to compare the string with the operator variable. The non-null string always equals to truth. More on that here: What is Truthy and Falsy? How is it different from True and False?.

if(operator != "*" and operator != "/" and operator != "+" and operator != "-" and operator != "%"): {
       print("please enter a valid operation")
}

Apart from that, the best way for you would be create a collection with the operations you want to perform and just check whether the operation provided by the user is there. For example with sets:

operations = {'+', '-', '*', '/', '%'}

if operator not in operations {
       print("please enter a valid operation")
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading