-2

in this code i first ask the user for the trigonometric function and then ask for the angle in radians or degrees. according to the code, the console should print error, but on running the code, the first if statements accept any input as true.the final coputed value is also worng. please suggest what to do and any other relevant changes that can be made to the code.

from math import *
trigFunc = str(input("What function do you want to use? Sine, Cosine or Tangent: "))
if trigFunc.lower == "sine" :
    radOrDeg = str(input("Do you want to input the value as radians or degrees? "))
    if radOrDeg.lower == "radians" :
        angle = int(input("Please input the value of the angle: "))
        print(math.sin(angle))
    elif radOrDeg.lower == "degrees" :
        angle = int(input("Please input the value of the angle: "))
        radAngle = int(math.radians(angle))
        print(math.sin(radAngle))
    else:
        print("error")
elif trigFunc.lower == "cosine" :
    radOrDeg = str(input("Do you want to input the value as radians or degrees? "))
    if radOrDeg.lower == "radians" :
        angle = int(input("Please input the value of the angle: "))
        print(math.cos(angle))
    elif radOrDeg.lower == "degrees" :
        angle = int(input("Please input the value of the angle: "))
        radAngle = int(math.radians(angle))
        print(math.cos(radAngle))
    else:
        print("error")
elif trigFunc.lower == "tangent" :
    radOrDeg = str(input("Do you want to input the value as radians or degrees? "))
    if radOrDeg.lower == "radians" :
        angle = int(input("Please input the value of the angle: "))
        print(math.tan(angle))
    elif radOrDeg.lower == "degrees" :
        angle = int(input("Please input the value of the angle: "))
        radAngle = int(math.radians(angle))
        print(math.tan(radAngle))
    else:
        print("error")
else:
    print("ERROR, the function cannot be used")
input("press enter to exit")
vaultah
  • 44,105
  • 12
  • 114
  • 143

2 Answers2

2

.lower is a function and you need to call it to return a string. Right now you are comparing a function to a string which will return False.

Change trigFunc.lower to trigFunc.lower().

https://docs.python.org/3/library/stdtypes.html#str.lower

Jacques Kvam
  • 2,856
  • 1
  • 26
  • 31
0

There are several errors in your code. Here are the things I changed to get your code working:

  1. Use trigFunc.lower() instead of trigFunc.lower. You need to call the method to get the result you desire.
  2. Use import math instead of from math import *, since you refer to math library throughout.
  3. Use math.radians(int(angle)) instead of int(math.radians(angle)), since math.radians does not take a string as an input.
jpp
  • 159,742
  • 34
  • 281
  • 339