-1

see this is my code

print("""1. ADD
    2. SUBTRACT
    3. MULTIPLY
    4. DIVIDE
    5. SQUARE 
    6. SQUARE ROOT
    7. CUBE
    8. CUBE ROOT
    9. POWER OF A NUMBER
    10. FACTORIAL
    11. TRIANGLE'S PERIMETER
    12. TRIANGLE'S AREA
    13. QUADRILATERAL'S PERIMETER
    14. PARALLELOGRAM'S AREA
    15. RHOMBUS'S AREA
    17. RECTANGLE'S AREA
    18. SQUARE'S AREA
    19. TRAPEZIUM'S AREA
    20. CIRCLE'S CIRCUMFERENCE
    21. CIRCLE'S AREA
    22. QUADRATIC EQUATION
    23. SIMPLE INTEREST
    24. COMPOUND INTEREST""")
while True:
  user = str(input("Enter GO to continue or Q to exit: "))
  if user == 'GO':
    user_input = str(input("Enter here the serial number or  the name in capital for what you want to calculate: "))
    if user_input == 'ADD' or 1 :
      add()
    elif user_input == 'SUBTRACT' or 2 :
      sub()
    elif user_input == 'MULTIPLY' or 3 :
      mlt()
    elif user_input == 'DIVIDE' or 4 :
      div()
    elif user_input == 'SQUARE' or 5 :
      sqr()
    elif user_input == 'SQUARE ROOT' or 6 :
      sqrt()
    elif user_input == 'CUBE' or 7 :
      cube()
    elif user_input == 'CUBE ROOT' or 8 :
      cube_root()
    elif user_input == 'POWER OF A NUMBER' or 9 :
      power()
    elif user_input == "TRIANGLE'S PERIMETER" or 11 :
      triangle_perimeter()
    elif user_input == "QUADRILATERAL'S PERIMETER" or 13 :
      quadrilateral_perimeter()
    elif user_input == "PARALLELOGRAM'S AREA" or 14 :
      parallelogram_area()
    elif user_input == 'FACTORIAL' or 10 :
      factorial()
    elif user_input == "RHOMBUS'S AREA" or 15 :
      rhombus_area()
    elif user_input == "RECTANGLE'S AREA" or 17 :
      rectangle_area()
    elif user_input == "SQUARE'S AREA" or 18 :
      square_area()
    elif user_input == "CIRCLE'S AREA" or 21 :
      circle_area()
    elif user_input == "CIRCLE'S CIRCUMFERENCE" or 20 :
      circle_circumference()
    elif user_input == "QUADRATIC EQUATION" or 22 :
      qaudratic_solver()
    elif user_input == "SIMPLE INTEREST" or 23 :
      simple_interest()
    elif user_input == "COMPOUND INTEREST" or 24 :
      compound_interest()
    elif user_input == "TRIANGLE'S AREA" or 12 :
      triangle_area()
    elif user_input == "TRAPEZIUM'S AREA" or 19 :
      trapezium_area()
    else:
      print("Invalid input!!")
  elif user == 'Q':
    break
  else:
    print("Invalid input!!")

and when I run it when I put any number or name in the input line instead of calling out the function with respect to the number or name it calls out to the add() function which relates number 1 and and name "ADD" Why is this happening? Please help me... PS: I had done this in google colab so the actual functions are written in a separate "cell" if you know google colab you'll know :) but anyways here are functions too

import cmath
import functools
def add():
  
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the first number: "))
      val2 = float(input("Enter the second number: "))
      print("The result is: ", val1 + val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def sub():
   
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the first number: "))
      val2 = float(input("Enter the second number: "))
      print("The result is: ", val1 - val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")
 
def mlt():
 
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the first number: "))
      val2 = float(input("Enter the second number: "))
      print("The result is: ", val1*val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def div():
 
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the first number: "))
      val2 = float(input("Enter the second number: "))
      print("The result is: ", val1/val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def sqrt():
  
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the number: "))
      print("The result is: ", cmath.sqrt(val1))
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def sqr():
   
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the number: "))
      print("The result is: ", val1**2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def power():
    
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the number: "))
      val2 = float(input("Enter the power of the number: "))
      print("The result is: ", val1**val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def square_area():

  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the value of the side: "))
      print("The area of the square is: ", val1**2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def rectangle_area():

  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the value of the length: "))
      val2 = float(input("Enter the value of the breadth: "))
      print("The area of the square is: ", val1*val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def circle_circumference():
    
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    
    if user_input == 'GO':
      val1 = float(input("Enter the radius of the circle: "))
      print("The circumference of the circle is: ", (val1*44)/7)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def circle_area():
    
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    
    if user_input == 'GO':
      val1 = float(input("Enter the radius of the circle: "))
      print("The area of the circle is: ", (val1*val1*22)/7)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def rhombus_area():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    
    if user_input == 'GO':
      
      val1 = float(input("Enter the value of first diagonal: "))
      val2 = float(input("Enter the value of the second diagonal: "))
      print("The area of the rhombus is: ", 0.5*val1*val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def parallelogram_area():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    
    if user_input == 'GO':
      
      val1 = float(input("Enter the value of height: "))
      val2 = float(input("Enter the value of the correspondent base: "))
      print("The area of the parallelogram is: ", 0.5*val1*val2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def quadrilateral_perimeter():
  while True:
   user_input = str(input("Enter GO to continue or Q to exit: "))
  
   if user_input == 'GO':
    
    a = float(input("Enter the first side of the quadrilateral: "))
    b = float(input("Enter the second side of the quadrilateral: "))
    c = float(input("Enter the third side of the quadrilateral: "))
    d = float(input("Enter the foruth side of the quadrilateral: "))
    if ((a + b + c> d) and (a + b + d> c) and (b + c + d> a) and (a + d + c> b)) :
           print("The perimeter of the qaudrilateral is : ", a+b+c+d)
    else :
            print("Quadrilateral is not valid.")
   elif user_input == 'Q':
     break
   else:
     print("invalid input")

def trapezium_area():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))

    if user_input == 'GO':
      par1 = float(input("Enter the value of the first parallel side: "))
      par2 = float(input("Enter the value of the second parallel side: "))
      hei1 = float(input("Enter the the value of the height: "))
      print("The area of the trapezium is: ", 0.5*hei1*(par1+par2))
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def triangle_perimeter():
  while True:
   user_input = str(input("Enter GO to continue or Q to exit: "))
  
   if user_input == 'GO':
    
    a = float(input("Enter the first side of the triangle: "))
    b = float(input("Enter the second side of the triangle: "))
    c = float(input("Enter the third side of the triangle: "))
    
    if ((a + b > c) and (a + c > b) and (b + c > a)) :
           print("The perimeter of the traingle is: ", a+b+c)
    else :
            print("Triangle is not valid.")
   elif user_input == 'Q':
     break
   else:
     print("invalid input")

def triangle_area():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))

    if user_input == 'GO':
      area_method = str(input("Enter Heron or Traditional(0.5*height*base): "))
      if area_method == 'Heron':
        a = float(input("Enter the first side of the triangle: "))
        b = float(input("Enter the second side of the triangle: "))
        c = float(input("Enter the third side of the triangle: "))
        if ((a + b > c) and (a + c > b) and (b + c > a)) :
          s = (a+b+c)*0.5
          print("The area of the triangle is ", cmath.sqrt((s-a)*(s-b)*(s-c)*(s)))
        else:
          print("Triangle is not valid!!")
      elif user_input == 'Traditional':
        a = float(input("Enter the value of the height: "))
        b = float(input("Enter the value of the correspondent base: "))
        print("The area of the triangle is ", 0.5*a*b)
      else:
        print("Invalid input!!")
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!") 

def qaudratic_solver(): 
  while True:
    user_input = str(input("Enter the GO to continue or Q to exit: "))
    if user_input == 'GO':
      a = float(input("Enter the coeffiecient of x^2: "))
      b = float(input("Enter the coeffiecient of x: "))
      c = float(input("Enter the constant: "))
      D = cmath.sqrt(b*b - 4*a*c)
      F = 2*a
      X1 = (-b + D)/(F)
      X2 = (-b - D)/(F)
      print("The values of x are: ", X1, ", ", X2)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def cube():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the number: "))
      print("The result is: ", val1**3)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def cube_root():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      val1 = float(input("Enter the number: "))
      print("The result is: ", val**1/3)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def compound_interest():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      P = float(input("Enter the principal amount: ")) 
      IR = float(input("Enter the interest rate: "))
      T = float(input("Enter the time period: "))
      N = float(input("Enter the number of times interest is compunded per unit time 't': "))
      A = P*((1+((IR/100)/N))**(N*T))
      CI = A-P
      print("The final amount will be ", A, " and the compund interest will total up to ", CI)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def simple_interest():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      P = float(input("Enter the principal amount: "))
      R = float(input("Enter the interest rate: "))
      T = float(input("Enter the time period: "))
      SI = (P*R*T)/100
      A = P + SI
      print("The final amount will be ", A, " and the simple interest will total up to ", SI)
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")

def factorial():
  while True:
    user_input = str(input("Enter GO to continue or Q to exit: "))
    if user_input == 'GO':
      a = int(input("Enter the number: "))
      print(functools.reduce(lambda x,y : x * y, range(1,a+1)))
    elif user_input == 'Q':
      break
    else:
      print("Invalid input!!")
martineau
  • 119,623
  • 25
  • 170
  • 301
  • @jonrsharpe: debatable. OP wants to test a single variable against multiple values. You may possibly still be able to use/massage those answers but there's a subtle difference. – paxdiablo Jun 17 '21 at 11:02
  • 1
    @paxdiablo feel free to add one of the others (see e.g. https://sopython.com/canon/22/why-doesn-t-if-x-a-or-b-or-c-do-what-i-expect/) but this has been covered time and time again and absolutely doesn't need yet another custom answer. – jonrsharpe Jun 17 '21 at 11:04
  • @jonrsharpe: that's a damn good link to know about, thanks for that. – paxdiablo Jun 17 '21 at 11:09

3 Answers3

0

The condition in:

if user_input == 'ADD' or 1:

doesn't quite do what you think it does.

Each side of an or is usually a separate sub-expression that is evaluated independently, then the results are or'ed together. The proper expression for your case would be:

    if user_input == 'ADD' or user_input == '1':

Note the quoted '1' in my version, the string '1' is not the same as the integer 1, and you are working with strings here.

However, a more Pythonic way would be:

if user_input in ['ADD', '1']:

which would allow you to keep code much shorter for things like:

if number in [2, 3, 5, 7, 11, 13, 17, 19]:
    print(f"{number} is a prime under twenty")

Interestingly enough, the result of an expression False or something is not necessarily True or False, it's actually something. You can see this with the following transcript:

>>> True or 'hello'
True

>>> False or 'hello'
'hello'

More detail can be found here:

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value.


When the something is an expression like a == b, it is a boolean value that comes out. But that's not the or doing that, it's the comparison.

That means the result of the expression you've used, user_input == 'ADD' or 1, will be True if the input was ADD, or 1 if not.

And it's not until you evaluate that 1 in a truthy context that it gets forced to True. Python's truthiness rules are basically everything is truthy except for:

  • empty strings.
  • empty collections (lists, tuples, dictionaries, sets, frozensets, and ranges).
  • zero of any numeric type (integer, floating point, or complex).
  • None.
  • user defined classed that implement the __bool__ method and decide to return a falsey value.
  • and, of course, False itself.

And, just as an aside, you should probably be aware that input() already gives you a string, there's no need to call str() on it.

In other words, the expression str(input("What? ")) is about as useful as int(42), harmless but unnecessary :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

The or statement is to check for another condition. Here with your or 1 you are checking if 1 != 0 (because in boolean is 0 is false and everything else is true). So what you need in the end is to check if user_input is 1.

You need to change your conditions line to if user_input == 'ADD' or user_input == '1' :

EDIT: as paxdiablo said if user_input in ['ADD', '1'] would be a more pythonic way to do it.

Hope this helps and you understood your mistake.

-1
if user_input == 'ADD' or 1 :

The above condition is evaluates to true due to 1.

JigarT
  • 1
  • 3