Jump to content

So basically I need a bit of help here. I'm taking an intro to computer science course at my high school, and have started using Python recently. This is apparently the first time the instructor has used Python, as well as mine, and I'm having issues with a program that's supposed to calculate the area of a given shape. Whenever I run it (in JetBrains' PyCharm), it doesn't throw any errors, however it completely ignores the all of the elif statements, and runs the code under the if statement regardless of whether or not eh condition is met. I have no idea what's causing this, and neither does the instructor, so I figured I'd try to see if any of the kind people on the LTT forums had any insight, in your infinite wisdom.

import math
print("Shape Area Calculator")
print("This calculator will only work work with circles, rectangles, triangles, and trapezoids.")
print("Do not enter units into the calculator.")

shape = input("Please Select A Supported Shape: ")
shape.lower()

if shape == "rectangle" or "square":
    top = int(input("Please enter the length of the top side: "))
    left = int(input("Please enter the length of the left side: "))
    area =  top * left
    print("The area of your rectangle is " + str(area))
elif shape == "triangle":
    bttm = int(input("Please enter the length of the bottom side: "))
    hgt =  int(input("Please enter the height of the triangle"))
    area = bttm * hgt / 2
    print("The area of your triangle is " + str(area))
elif shape == "trapezoid":
    top = int(input("Please enter the length of the longest parallel: "))
    bttm = int(input("Please enter the length of the shortest parallel: "))
    hgt =  int(input("Please enter the height of the trapezoid: "))
    area = top + bttm / 2 * hgt
    print("The Area of Your Trapezoid is " + str(area))
elif shape == "circle":
    r = int(input("Please enter the radius of the circle: "))
    area = math.pi * r
    print("The Area of Your Circle is " + str(area))
else:
    print("Sorry, this shape is not supported.")

Running that yields the following [the "(input by me)" parts have been added just to show what and where inputs were being made.

"C:\Program Files (x86)\Python35-32\python.exe" C:/Users/walkerre/PycharmProjects/PythonUnit/calc2.py
Shape Area Calculator
This calculator will only work work with circles, rectangles, triangles, and trapezoids.
Do not enter units into the calculator.
Please Select A Supported Shape: circle (input by me)
Please enter the length of the top side: 8 (input by me)
Please enter the length of the left side: 8 (input by me)
The area of your rectangle is 64

Process finished with exit code 0

In fact, the above always occurs regardless of what shape I input. The math seems to be working, however.

Link to comment
https://linustechtips.com/topic/596818-conditional-statements-in-python/
Share on other sites

Link to post
Share on other sites

if shape in {"rectangle", "square"}:
	....


if (shape == "rectangle") or (shape == "square"):
	....

 

A good explenation

http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values

 

what you had befor evaluated into:

 

if shape == "rectangle" or "square" != 0

if shape is equal to "rectangle" OR if "square" is not equal to 0

 

if "square" is not equal to 0, is always true

Link to post
Share on other sites

8 minutes ago, Hamosch said:

if shape in {"rectangle", "square"}:
	....


if (shape == "rectangle") or (shape == "square"):
	....

 

A good explenation

http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values

 

what you had befor evaluated into:

 

if shape is equal to rectangle OR if square

 

if "square"is always true

I really really love you right now, you are amazing. Thank you so much. Much clearer now.

Link to post
Share on other sites

I wouldn't recommend using strings to identify what is what. There is a greater chance of accidentally writing the wrong string and they take up more memory. Integers would be better from that respect but could also be more difficult to read.

 

Edit: Nvm. I read you're program and changing the input to integers would take more work.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×