Ok I've started programming 3 days ago. I decided to choose python3 as my language because its pretty easy to learn yet reasonably powerful. I've used a variety of resources on the net, most of which were originally made for python2. I've used my new found skills to make a simple program that can calculate the area of various 2d shapes. I'd like your expereanced eyes to asses my code. Be as harsh as you want but please be specific and offer me ways in which I can improve my coding. Heres the code:
shape = ("triangle","square","rectangle","circle") # This creates a tuple containing the string names of all the shapes to be calculated .
constants = (3.14) # This stores the value of pi. Later I will add more constants to it and make it a tuple.
start_up = input("Do you want to calculate a shape's area?: ") # Fairly self explanatory.
if start_up != ("yes" or "no"): # This is to prevent the program failing if the user answers the start_up question.
print("Sorry but I don't understand what your saying") # with an answer outside of the simple yes/no that it requires.
start_up = input("Do you want to calculate a shape's area?: ") # re-iterates the start up question.
start_up = input("Do you want to calculate a shape's area?: ") # re-iterates the start up question.
while start_up == "yes": # This is the condition that obviously contains the bulk of the program.
target_shape = input("Choose your shape: ") # Once again, self explainatory.
if target_shape == shape[0]: # Uses the shape tuple to bring forth the case for the first shape, the triangle.
h = float(input("give the height: ")) # These Two lines allow the user to input the required values to calculate the area of the triangle.
b = float(input("give the base length: ")) # They are using the float funtion to increase the accuracy of the answer.
area_triangle = h * 1/2 * b # The formula we all learned in school.
print("the area of the %s is %.2f" % (shape[0],area_triangle)) # This generates a format string which uses both the shape tuple and the area_triangle object
# to display the answer. note the %.2f operand used to specify how many decimal places for the answer.
if target_shape == shape[1]: # Square
l = float(input("give the length: "))
area_square = l ** 2
print("the area of the %s is %.2f" % (shape[1],area_square))
if target_shape == shape[2]: # Rectangle
l = float(input("give the length: "))
w = float(input("give the width: "))
area_rectangle = l * w
print("the area of the %s is %.2f" % (shape[2],area_rectangle))
if target_shape == shape[3]: # Circle
r = float(input("give the radius: "))
pi = constants # In this line we are calling forth the constants object to use the value of pi.
area_circle = 2 * pi * r
print("the area of the %s is %.2f" %(shape[3],area_circle))
start_up = input("Do you want to calculate another shape's area?: ") # This line allows the user the chance to do just what it says in the brackets.
else: # This is to safegaurd against failure in the event of the user
print("That shape's not in my database") # inputing a shape non-existant in the shape tuple.
start_up = input("Do you want to calculate another shape's area?: ")
if start_up == "no": #This is for when the user no longer wants to use the program.
print("Goodbye then!")
input("Press any key to close")
Edit: Ok thanks for all the helpful comments. Here's my revised code. I've tried to take on board the advice of everyone to the best of my ability.
while True:
startup = None
while startup not in ("yes", "no"):
if startup is not None:
print("Sorry but I don't know what you are saying.")
startup = input("Do you want to calculate a shape's area?: ").lower()
if startup == "no":
print("Goodbye then!")
input("Press enter to close")
break
elif startup == "yes":
print("Cool!")
shape = None
while shape not in ("triangle", "square", "rectangle", "circle"):
if shape is not None:
print("""sorry but I don't know that shape.
That is if it even is a shape. -_-""")
shape = input("What shape?: ").lower()
if shape == "triangle":
print("Ok you need to give me the height and the base length")
h = float(input("height = ")); b = float(input("base = "))
area = h * 1/2 * b
elif shape == "square":
print("Ok I just need the length of a side from you")
l = float(input("length = "))
area = l ** 2
elif shape == "rectangle":
print("give me the length and width")
l = float(input("length = ")); w = float(input("width = "))
area = l * w
elif shape == "circle":
print("I just need the radius")
r = float(input("radius = "))
area = 2 * 3.14 * r
print("The area = "); print(area)
Note that there is one huge problem in this code that I still haven't resolved. As Justin had pointed out, if someone types in an invalid value or even nothing at all for the area formulas, the program will fail. I've tried to fix this but I can't. Any help here would be great. Also I tried to substitue 3.14 with math.pi like Cygal suggested but it seemed to make my program no longer work. I'm pretty sure I'm just using it wrong.
{}button. It really couldn't be easier. And no, it doesn't work as intended, at the very leastif start_up != "yes" or "no":is always true. – Wooble Sep 13 '12 at 14:06if start_up != ("yes" or "no"):is still wrong;('yes' or 'no')is'yes'. You're looking fornot inor 2 inequality tests. – Wooble Sep 13 '12 at 16:55