The code is written in python 3.3 and only works for the first if statement, not acknowledging the other elif statements even if the if statement is wrong.
calccircle()
What data do you know? radius
Enter Diameter def calccircle():
x = input("What data do you know? ")
if x == "Diameter" or "diameter":
a = int(input("Enter Diameter "))
print("Circumference is", a * math.pi)
print("Area is", math.pi * math.pow(a/2,2))
print("Radius is:",a/2)
elif x == "Radius" or "radius":
b = input("Enter radius: ")
print("Circumference is", b * 2 * math.pi)
print("Area is", math.pi * math.pow(b,2))
print("Diameter is", b * 2)
elif x == "area" or "Area":
c = input("Enter area: ")
print("Circumference is", ((math.sqrt(c))/math.pi) * b * 2 * math.pi)
print("Diameter is", math.sqrt(c) * math.pi * 2)
print("Radius is", math.sqrt(c) * math.pi)
elif x == "circumference" or "Circumference":
d = input("Enter Circumference: ")
print("Area is", math.pi * math.pow(d/math.pi,2))
print("Diameter is", d/math.pi * 2)
print("Radius is", d/math.pi)
It displays the input(“Enter diameter: “) and doesn’t pay attention to what I write or the if statements.
calccircle()
What data do you know? radius
Enter Diameter
Notice I wrote radius, and the input(“Enter radius: “) is supposed to run, but it doesn’t. Please help.
Your problem is this:
looks to python like:
which, when
x != "Diameter", is like:which will always go through.
Python treats
False, None, "", [], {}, ...and the like asFalsein the context ofifstatements (or if you callboolon them, or various other places), and most everything else asTrue. This is often handy, but can combine with a slight confusion about theorstatement to make lots of people not used to Python make this mistake.Instead, do one of these:
It’s also worth noting that if you wrote something like
this would be the same as
since
"Diameter" or "diameter"sees that"Diameter"isTrueish, and so returns that.