Q6
unknown
python
3 years ago
6.6 kB
9
Indexable
import math
# check if number is positive or not
def isPositive(num):
if num < 0:
return False
else:
return True
# this func calculate
# the area and circumference of a circle
def circle(fileLine):
if not userInput:
shape.write(str(fileLine))
fileLine = tempShape.readline()
shape.write(fileLine)
fileLine = fileLine.rstrip('\n')
try:
r = float(fileLine)
if not isPositive(r):
if not userInput:
shape.write("Error " + str(r) + " need to be a positive number " + '\n')
raise ValueError("need to be a positive number ")
else:
if userInput:
shape.write(shapeName + '\n')
area = r ** 2 * math.pi
circumference = 2 * math.pi * r
shape.write(str(area) + '\n')
shape.write(str(circumference) + '\n')
except ValueError as ve:
print(ve)
# this func calculate
# the area and circumference of a rectangle
# a= The length of the first side of the rectangle
# b= The length of the second side of the rectangle
def rectangle(fileLine):
if not userInput:
shape.write(str(fileLine))
tempA = tempShape.readline()
shape.write(tempA)
tempB = tempShape.readline()
shape.write(tempB)
else:
tempA = input("Enter the first side: ")
tempB = input("Enter the second side: ")
try:
a = float(tempA)
b = float(tempB)
if a % 1 != 0:
if not userInput:
shape.write("Error " + str(a) + " needs to be an integers " + '\n')
raise ValueError(str(a) + "need to be an integer ")
elif b % 1 != 0:
if not userInput:
shape.write("Error " + str(b) + " need to be an integer " + '\n')
raise ValueError(str(b) + "need to be an integer ")
elif not isPositive(a):
if not userInput:
shape.write("Error " + str(a) + " needs to be a positive numbers " + '\n')
raise ValueError(str(a) + "need to be a positive number ")
elif not isPositive(b):
if not userInput:
shape.write("Error " + str(b) + " need to be a positive number " + '\n')
raise ValueError(str(b) + "need to be a positive number ")
else:
if not userInput:
area = int(a * b)
circumference = int(2 * a + 2 * b)
shape.write(str(area) + '\n')
shape.write(str(circumference) + '\n')
else:
if userInput:
shape.write(fileLine + '\n')
shape.write(tempA + '\n')
shape.write(tempB + '\n')
area = float(a * b)
circumference = float(2 * a + 2 * b)
shape.write(str(area) + '\n')
shape.write(str(circumference) + '\n')
except ValueError as ve:
print(ve)
# this func calculate
# the area and circumference of a triangle
# a= Base length of a triangle
# b= the height of the triangle
def triangle(fileLine):
if not userInput:
shape.write(str(fileLine))
tempA = tempShape.readline()
shape.write(tempA)
tempB = tempShape.readline()
shape.write(tempB)
else:
tempA = input("Enter the base length of a triangle: ")
tempB = input("Enter the height of the triangle: ")
try:
a = float(tempA)
b = float(tempB)
if a % 1 != 0:
if not userInput:
shape.write("Error " + str(a) + " needs to be an integers " + '\n')
raise ValueError(str(a) + "need to be an integer ")
elif b % 1 != 0:
if not userInput:
shape.write("Error " + str(b) + " need to be an integer " + '\n')
raise ValueError(str(b) + "need to be an integer ")
elif not isPositive(a):
if not userInput:
shape.write("Error " + str(a) + " needs to be a positive numbers " + '\n')
raise ValueError(str(a) + "need to be a positive number ")
elif not isPositive(b):
if not userInput:
shape.write("Error " + str(b) + " need to be a positive number " + '\n')
raise ValueError(str(b) + "need to be a positive number ")
else:
if userInput:
shape.write(fileLine + '\n')
shape.write(tempA + '\n')
shape.write(tempB + '\n')
area = float((a * b) / 2)
c = math.sqrt(a ** 2 + b ** 2)
circumference = float(a + b + c)
shape.write(str(area) + '\n')
shape.write(str(circumference) + '\n')
except ValueError as ve:
print(ve)
userInput = False
shape = open('Shapes.txt', 'r')
tempShape = open('tempShapes', 'w')
line = shape.readline()
# we read the data from the shape.txt to temp file
while line != '':
tempShape.write(line)
line = shape.readline()
shape.close()
tempShape.close()
shape = open('Shapes.txt', 'w')
tempShape = open('tempShapes', 'r')
line = tempShape.readline()
# this while we write back to the shape.txt the correct data
while line != '':
tempLine = line
tempLine = line.rstrip('\n')
if tempLine.lower() == "circle":
circle(line)
elif tempLine.lower() == "rectangle":
rectangle(line)
elif tempLine.lower() == "triangle":
triangle(line)
line = tempShape.readline()
shape.close()
tempShape.close()
shape = open('Shapes.txt', "a")
stop = False
# this while we get the input from the user
while not stop:
userInput = True
try:
shapeName = input("Enter name of shape")
tempShapeName = shapeName
if tempShapeName.lower() == "circle":
radius = (input("Enter the radius"))
circle(radius)
elif tempShapeName.lower() == "rectangle":
rectangle(shapeName)
elif tempShapeName.lower() == "triangle":
triangle(shapeName)
else:
raise Exception("The shape you enter are wrong please try again")
key = input("you want to stop the program? press 'n'"'\n'"if not press any key: ")
if key == "n":
stop = True
shape.close()
except Exception as ex:
print(ex)
Editor is loading...