Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #20th-25th November 2015
- #Homework
- #setting values of variables.
- def abcdx():
- print "a - Square"
- print "b - Rectangle"
- print "c - Triangle"
- print "d - Circle"
- print "x - Exit"
- #importing the function.
- from math import pi
- import sys
- def chooseoperation(): #choosing which operation you want to use
- global choiceofoperation
- choiceofoperation = raw_input("Please tell me which operation you want to use.")
- choiceofoperation = choiceofoperation.lower()
- #for working out the square
- def square():
- print "You are working with a square"
- sidelength = float(raw_input("Please enter the length of a side of this square."))
- area = sidelength ** 2
- perimeter = sidelength * 4 #equation to figure out what you need
- print "The area of this square (with side length", str(sidelength) + ") is:", str(area), ", And the perimeter of this square is: ", str(perimeter)
- #working out of a square using def to set its own variables
- def rectangle():
- print "You are working with a rectangle"
- shortersidelength = float(raw_input("Please tell me the length of one of the two shorter sides."))
- longersidelength = float(raw_input("Please tell me the length of one of the two longer sides."))
- area = shortersidelength * longersidelength
- perimeter = (shortersidelength * 2) + (longersidelength * 2)
- print "The area of this rectangle (with shorter side length", str(shortersidelength) + ", and longer side length", str(longersidelength), "is:", str(area), "And the perimeter of this rectangle is:", str(perimeter)
- #working out for a triangle.
- def triangle():
- print "You are working with a triangle"
- longestlength = float(raw_input("Please tell me the length of the longest side of the triangle."))
- secondlongestlength = float(raw_input("Please tell me the length of the second longest side of the triangle."))
- thirdlongestlength = float(raw_input("Please tell me the length of the third longest (AKA shortest) side of the triangle."))
- area = (longestlength * secondlongestlength) / 2
- perimeter = longestlength + secondlongestlength + thirdlongestlength
- print "The area of this triangle (with longest length", str(longestlength) + ", second longest length", str(secondlongestlength), "and shortest length", str(thirdlongestlength) + "), is:", str(area), "And the perimeter of this triangle is:", str(perimeter)
- def circle():
- print "You are working with a circle!"
- radius = float(raw_input("Please give me the radius of the circle."))
- area = pi * (radius ** 2)
- circumference = pi * (radius * 2)
- print "The area of this circle (with the radius", str(radius) + ") is:", str(area), "And the circumference of this circle is:", str(circumference)
- def exit():
- print "Okay!"
- sys.exit()
- while True:
- chooseoperation()
- while choiceofoperation not in ("a","b","c","d","x"):
- print "You did not make an acceptable choice."
- abcdx()
- chooseoperation()
- if choiceofoperation == "a":
- square()
- elif choiceofoperation == "b":
- rectangle()
- elif choiceofoperation == "c":
- triangle()
- elif choiceofoperation == "d":
- circle()
- elif choiceofoperation == "x":
- print "Okay, exiting!"
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement