-1

So, I created a simple program with an if and elif statement. However, no matter what I input, the code will read it as if I inputted A, which means no matter what it'll ask me to enter in a radians value. How could I go about fixing this?

import math

pi = math.pi


print("Do you wish to...")
print("(A)Convert radians to degrees.")
print("(B)Convert degrees to radians.")

choice = input(" ")

if choice == "A" or "a":
    rads = int(input("Enter in your radians value:\n"))
    degrees = rads * 180/pi
    print(rads, "radians = ", degrees," degrees")
elif choice == "B":
     degs = int(input("Enter in you degree value:\n"))
     radians = degs * pi/180
     print(degs, "degrees = ", radians," radians")

2 Answers2

0

You have to be explicit with your comparisons. The string "a" will evaluate to True (a non-zero length string) in your if statement. So this line...

if choice == "A" or "a":

is saying if (choice == "A") or (True) which will always evaluate to True. What you want is...

if choice == "A" or choice == "a":
Birk
  • 11
  • 2
0

The problem is that choice == "A" or "a" is equivalent to (choice == "A") or bool("a").

And bool("a") is always True, so your if statement is unfalsifiable.

Instead, replace it with any of these options:

  • if choice.upper() == "A":
  • if choice in ("A", "a"):
  • if choice == "A" or choice == "a":
Aaron V
  • 6,596
  • 5
  • 28
  • 31