1

I am using Python in an attempt to create an Area Calculator (similar to the one from Code Academy). Only my else statement seems to be running:

print("Area Calculator.")
print ("Select Shape:")
print ("Circle or Triangle? ")

answer = input()

if answer == "Circle" or "C" or "c" or "circle":
    radius = float(input("Input Radius: "))
    area_c = (3.12159 * radius) * 2
    print (area_c)

elif answer == "Triangle" or "T" or "t" or "triangle":
    base = float(input("Input Base: "))
    height = float(input("Input Height: "))
    area_t = (.5 * base) * height
    print (area_t)

else:
    print ("error")

I edit texts using PyCharm and no syntax errors or errors of any other kind return. Regardless of what I respond into the answer input (whether they are integers or syntax) the code always displays line 8 [radius = float(input("Input Radius: "))]

I am sorry if this turns out to be an easy fix. I am just getting started with Python and have tried a variety of indentation and syntactical variations to no avail.

smci
  • 32,567
  • 20
  • 113
  • 146
  • 2
    `if answer == "Circle" or answer == "C" or answer == "c" or answer =="circle":`, make the same changes to your elif statement. – Heaven Oct 14 '18 at 00:48

3 Answers3

0

Use:

print("Area Calculator.")
print ("Select Shape:")
print ("Circle or Triangle? ")

answer = input()

if answer.lower() in {"circle","c"}:
    radius = float(input("Input Radius: "))
    area_c = (3.12159 * radius) * 2
    print (area_c)

elif answer.lower() in {"triangle","t"}:
    base = float(input("Input Base: "))
    height = float(input("Input Height: "))
    area_t = (.5 * base) * height
    print (area_t)

else:
    print ("error")

The changes are the lines with or, use in instead so checks by the set

That's what's different.

Note use lower for simplifying the length

Note use set for speed, (it's faster)

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Thank you U9-Forwad for helping me with this code. I do have one question if you don't mind, there is one thing I don't understand. What do you mean by "Note use set for speed, (it's faster)"? I know that by sets you mean the swigly lines as opposed to parentheses but what is the utility in using sets as opposed to just parentheses? I have searched the information on the Python website and another coding site but I still don't understand what they mean. Thank you again very much for assisting me! – Joseph Tadros Oct 15 '18 at 02:54
  • @JosephTadros Oh remember, i looked the duplicate question's accepted answer, it says "using a set to take advantage of the constant-cost membership test (in takes a fixed amount of time whatever the left-hand operand is)." – U13-Forward Oct 15 '18 at 03:21
-1

You are using the == operator incorrectly. You have to use it this way:

if answer == "Circle" or answer == "C" or answer == "c" or answer == "circle":

An easier way to do this will be to using check whether your string matches any of the items in a tuple or list. So your code will need to be modified like this:

print("Area Calculator.")
print ("Select Shape:")
print ("Circle or Triangle? ")

answer = input()

if answer in ("Circle", "C", "c", "circle"):
    radius = float(input("Input Radius: "))
    area_c = (3.12159 * radius) * 2
    print (area_c)

elif answer in ("Triangle", "T", "t", "triangle"):
    base = float(input("Input Base: "))
    height = float(input("Input Height: "))
    area_t = (.5 * base) * height
    print (area_t)

else:
    print ("error")
  • Thank you very much Mr. Rastogi! I have implimented the suggested modifications and my code now runs exactly how I had intended it to run! – Joseph Tadros Oct 15 '18 at 02:42
  • @JosephTadros Glad it worked! Remember to "accept" whichever answer was the most helpful to you. Doesn't have to be mine. – Mayank K Rastogi Oct 15 '18 at 15:24
-2
if answer == "Circle" or answer == "C" or answer == "c" or answer == "circle":
vdubsterrr
  • 27
  • 2
  • 1
    You need explanation, no beginners will understand, they can't rap there head around this – U13-Forward Oct 14 '18 at 00:53
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Oct 14 '18 at 01:25