แบบที่ 3
def calculateArea(area,check):
____print "Program to calculate Area of ",area
____if check == "r" or check == "t":
________base = getUserInput("Please enter base -> ")
________height = getUserInput("Please enter height -> ")
________if check == "r" :
____________print "Rectangle area of base=", base, "and height=", height, "is", \
__________________base*height
________elif check == "t" :
____________print "Triangle area of base=", base, "and height=", height, "is", \
__________________0.5*base*height
____elif check == "c":
________radius = getUserInput("Please enter radius -> ")
________print "Circle area of radius=", radius, "is", 3.14159*(radius*radius)
____elif check == "z":
________parallel_base1 = getUserInput("Please enter parallel base 1-> ")
________parallel_base2 = getUserInput("Please enter parallel base 2-> ")
________height = getUserInput("Please enter height -> ")
________print "Trapezoids area of parallel base1=", parallel_base1, '\n'\
______________"parallel base2=", parallel_base2, '\n'"and height=", height,"is",\
______________0.5*(parallel_base1+parallel_base2)*height
def getUserInput(text_to_display):
____input = raw_input(text_to_display)
____return int(input)
choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateArea("Rectangle","r")
elif choice == "T" or choice == "t":
____calculateArea("Triangle","t")
elif choice == "C" or choice == "c":
____calculateArea("Circle","c")
elif choice == "Z" or choice == "z":
____calculateArea("Trapezoids","z")
else:
____print "Please type R,T,C,Z to choose the type of area to calculate "
แบบที่ 4
def getUserInput(text_to_display):
____input = raw_input(text_to_display)
____return int(input)
def calculateAREA(Area_of):
____print "Program to calculate Area of", Area_of
____if Area_of == "Circle":
________radious = getUserInput("Please enter radious -> ")
________result = 3.14159*radious*radious
____elif Area_of == "Trapezoid":
________parallel_line1 = getUserInput("Please enter length of 1st parallel line -> ")
________parallel_line2 = getUserInput("Please enter length of 2nd parallel line -> ")
________height = getUserInput("Please enter height -> ")
________result = (parallel_line1+parallel_line2)*height*0.5
____else:
________base = getUserInput("Please enter base -> ")
________height = getUserInput("Please enter height -> ")
________if Area_of == "Rectangle":
____________result = base*height
________else:
____________result = base*height*0.5
____print Area_of, "area is", result
i = 0
while i == 0:
____choice = raw_input("Which type of area you would like to calculate\
_______________________(R,T,Z or C)? -> ")
____if choice == "R" or choice == "r":
________calculateAREA("Rectangle")
________i += 1
____elif choice == "T" or choice == "t":
________calculateAREA("Triangle")
________i += 1
____elif choice == "C" or choice == "c":
________calculateAREA("Circle")
________i += 1
____elif choice == "Z" or choice == "z":
________calculateAREA("Trapezoid")
________i += 1
____else:
________print "Please type R,T,Z or C to choose the type of area to calculate."
แบบที่ 5
def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radius = getUserInput("Please enter radius -> ")
____print "Circle area of radius", radius ,"is",3.14159*(radius**2)
def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids"
____parallel_1 = getUserInput("Please enter parallel_base1 -> ")
____parallel_2 = getUserInput("Please enter parallel_base2 -> ")
____height = getUserInput("Please enter height -> ")
____print "Trapezoids area of parallel_base1", parallel_1,\
__________"\nTrapezoids area of parallel_base2", parallel_2,"\nand height", height,\
__________"\nis", 0.5*(parallel_1+parallel_2)*height
def getUserInput(text_to_display):
____input = raw_input(text_to_display)
____return int(input)
def baseadnheight(area):
____print "Program to calculate Area of ",area
____base = getUserInput("Please enter base -> ")
____height = getUserInput("Please enter height -> ")
____if(area=="Rectangle"):
________a = base*height
____else:
________a = base*height*0.5
____print area," area of base", base,"and height", height,"is", a
choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____baseadnheight("Rectangle")
elif choice == "T" or choice == "t":
____baseadnheight("Triangle")
elif choice == "C" or choice == "c":
____calculateCircleArea()
elif choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
else:
____print "Please type R, T, C or Z to choose the type of area to calculate"