我需要帮助让它问房间的形状,下面列出的房间,他们想要的楼层类型,下面列出,并询问这些有多少房间正在计算。同时打印每个房间的价格,以及所有房间的总价格。运行它的结果低于所有代码。
carpet = 25
vinyl = 17
hardwood = 31
concrete = 18
def circle():
r = float(input('What is the radius of the room?: '))
pi = 3.14
room_size = pi * r **2
return (room_size)
def rectangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = b * h
return (room_size)
def regular_polygon():
p = ('What is the perimeter of the room?: ')
a = ('What is the Apothegm of the room?: ')
room_size = p * a / 2
return (room_size)
def trapezoid():
b1 = float(input('What is the length of the bottom base of the room?: '))
b2 = float(input('What is the length of the top base of the room?: '))
h = float(input('How far apart are the top base and bottom base?: '))
room_size = h * (b1 + b2) / 2
return (room_size)
def triangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = (b * h) / 2
return (room_size)
def floor(floorings):
floorings = input('What floor type would you like?: carpet, vinyl, hardwood, or concrete.')
if floorings == ('carpet'):
floor_type = carpet
elif floorings == ('vinyl'):
floor_type = vinyl
elif floorings == ('hardwood'):
floor_type = hardwood
elif floorings == ('concrete'):
floor_type = concrete
else:
raise ValueError('Invalid room shape: ' + equation_type)
return floor_type
number_of_rooms = int(input('How many rooms will we be calculating?: '))
def rooms(room_shape):
total_area = 0
for i in range(room_shape):
equation_type = input('What is the shape of the room (circle, rectangle, regular polygon, trapezoid, triangle)? ').lower()
if equation_type == ('circle'):
total = (total_area + circle())
elif equation_type == ('rectangle'):
total = (total_area + rectangle())
elif equation_type == ('regular polygon'):
total = (total_area + regular_polygon())
elif equation_type == ('trapezoid'):
total = (total_area + trapezoid())
elif equation_type == ('triangle'):
total = (total_area + triangle())
else:
raise ValueError('Invalid room shape: ' + equation_type)
return total
room_count = number_of_rooms
print (('$'), rooms(room_count) * floor)
房间的宽度是多少?房间的形状是什么(圆形、矩形、正多边形、梯形、三角形)?矩形房间的长度是多少?:5房间的宽度是多少?:2回溯(最近一次调用):File“/Users/···/Desktop/coputer coding/Flooring estimator.py",第100行,打印中(('$'),rooms(room_count) * floor) TypeError:*:'float‘和’floor‘不支持的操作数类型
发布于 2020-12-16 18:19:05
您当前的代码有两个问题:
1. print (('$'), rooms(room_count) * floor)
这里的floor函数是通过编写floor
调用的,而它实际上应该是floor()
2. floor(floorings)
您不需要floorings
参数,因为您将该参数作为来自用户的输入。
这是更正后的代码:
carpet = 25
vinyl = 17
hardwood = 31
concrete = 18
def circle():
r = float(input('What is the radius of the room?: '))
pi = 3.14
room_size = pi * r **2
return (room_size)
def rectangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = b * h
return (room_size)
def regular_polygon():
p = ('What is the perimeter of the room?: ')
a = ('What is the Apothegm of the room?: ')
room_size = p * a / 2
return (room_size)
def trapezoid():
b1 = float(input('What is the length of the bottom base of the room?: '))
b2 = float(input('What is the length of the top base of the room?: '))
h = float(input('How far apart are the top base and bottom base?: '))
room_size = h * (b1 + b2) / 2
return (room_size)
def triangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = (b * h) / 2
return (room_size)
def floor():
floorings = input('What floor type would you like?: carpet, vinyl, hardwood, or concrete : ')
if floorings == ('carpet'):
floor_type = carpet
elif floorings == ('vinyl'):
floor_type = vinyl
elif floorings == ('hardwood'):
floor_type = hardwood
elif floorings == ('concrete'):
floor_type = concrete
else:
raise ValueError('Invalid room shape: ' + equation_type)
return floor_type
number_of_rooms = int(input('How many rooms will we be calculating?: '))
def rooms(room_shape):
total_area = 0
for i in range(room_shape):
equation_type = input('What is the shape of the room (circle, rectangle, regular polygon, trapezoid, triangle)? ').lower()
if equation_type == ('circle'):
total_area = (total_area + circle())
elif equation_type == ('rectangle'):
total_area = (total_area + rectangle())
elif equation_type == ('regular polygon'):
total_area = (total_area + regular_polygon())
elif equation_type == ('trapezoid'):
total_area = (total_area + trapezoid())
elif equation_type == ('triangle'):
total_area = (total_area + triangle())
else:
raise ValueError('Invalid room shape: ' + equation_type)
return total_area
room_count = number_of_rooms
print (('$'), rooms(room_count) * floor())
下面是更正后的代码的执行说明
number_of_rooms
变量中读取。rooms(room_shape)
函数计算得出的。rooms(room_shape)
函数对所有房间重复一组步骤。步骤是计算每个房间的楼层面积,并将其添加到使用total_area
变量跟踪的总楼层面积中。total_area
的值。floor
变量返回的值中。从而为您提供最终成本。然而,这个地板成本只添加了一次,并且根本不是按照您期望的计算方式计算的。我建议为每个房间计算它,每个房间可能有不同类型的地板,例如
total_cost = room_area1 * floor1 + room_area2 * floor .... so on
为此,您应该使用如下所示的代码:
carpet = 25
vinyl = 17
hardwood = 31
concrete = 18
def circle():
r = float(input('What is the radius of the room?: '))
pi = 3.14
room_size = pi * r **2
return room_size * floor()
def rectangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = b * h
return room_size * floor()
def regular_polygon():
p = ('What is the perimeter of the room?: ')
a = ('What is the Apothegm of the room?: ')
room_size = p * a / 2
return room_size * floor()
def trapezoid():
b1 = float(input('What is the length of the bottom base of the room?: '))
b2 = float(input('What is the length of the top base of the room?: '))
h = float(input('How far apart are the top base and bottom base?: '))
room_size = h * (b1 + b2) / 2
return room_size * floor()
def triangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = (b * h) / 2
return room_size * floor()
def floor():
floorings = input('What floor type would you like?: carpet, vinyl, hardwood, or concrete : ')
if floorings == ('carpet'):
floor_type = carpet
elif floorings == ('vinyl'):
floor_type = vinyl
elif floorings == ('hardwood'):
floor_type = hardwood
elif floorings == ('concrete'):
floor_type = concrete
else:
raise ValueError('Invalid room shape: ' + equation_type)
return floor_type
number_of_rooms = int(input('How many rooms will we be calculating?: '))
def rooms(room_shape):
total_area = 0
for i in range(room_shape):
equation_type = input('What is the shape of the room (circle, rectangle, regular polygon, trapezoid, triangle)? ').lower()
if equation_type == ('circle'):
total_area = (total_area + circle())
elif equation_type == ('rectangle'):
total_area = (total_area + rectangle())
elif equation_type == ('regular polygon'):
total_area = (total_area + regular_polygon())
elif equation_type == ('trapezoid'):
total_area = (total_area + trapezoid())
elif equation_type == ('triangle'):
total_area = (total_area + triangle())
else:
raise ValueError('Invalid room shape: ' + equation_type)
return total_area
room_count = number_of_rooms
print (('$'), rooms(room_count))
这里唯一的变化是,每个房间都调用floor()
,而不是在计算净面积后调用一次。
但是当前的代码仍然存在一个问题,即不能正确使用total
和total_count
来添加和更新网络区域。相反,要获得单个房间的价格,您可以使用以下方法:
carpet = 25
vinyl = 17
hardwood = 31
concrete = 18
def circle():
r = float(input('What is the radius of the room?: '))
pi = 3.14
room_size = pi * r **2
return room_size * floor()
def rectangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = b * h
return room_size * floor()
def regular_polygon():
p = ('What is the perimeter of the room?: ')
a = ('What is the Apothegm of the room?: ')
room_size = p * a / 2
return room_size * floor()
def trapezoid():
b1 = float(input('What is the length of the bottom base of the room?: '))
b2 = float(input('What is the length of the top base of the room?: '))
h = float(input('How far apart are the top base and bottom base?: '))
room_size = h * (b1 + b2) / 2
return room_size * floor()
def triangle():
b = float(input('What is the length the room?: '))
h = float(input('What is the width of the room?: '))
room_size = (b * h) / 2
return room_size * floor()
def floor():
floorings = input('What floor type would you like?: carpet, vinyl, hardwood, or concrete : ')
if floorings == ('carpet'):
floor_type = carpet
elif floorings == ('vinyl'):
floor_type = vinyl
elif floorings == ('hardwood'):
floor_type = hardwood
elif floorings == ('concrete'):
floor_type = concrete
else:
raise ValueError('Invalid room shape: ' + equation_type)
return floor_type
number_of_rooms = int(input('How many rooms will we be calculating?: '))
def rooms(room_shape):
total_area = []
for i in range(room_shape):
equation_type = input('What is the shape of the room (circle, rectangle, regular polygon, trapezoid, triangle)? ').lower()
if equation_type == ('circle'):
total_area.append(circle())
elif equation_type == ('rectangle'):
total_area.append(rectangle())
elif equation_type == ('regular polygon'):
total_area.append(regular_polygon())
elif equation_type == ('trapezoid'):
total_area.append(trapezoid())
elif equation_type == ('triangle'):
total_area.append(triangle())
else:
raise ValueError('Invalid room shape: ' + equation_type)
return total_area
room_count = number_of_rooms
rooms_cost = rooms(room_count)
for room_cost in range(len(rooms_cost)):
print (f"The cost for room {room_cost+1} is ${rooms_cost[room_cost]}")
print("\nTotal Cost : ", sum(rooms_cost))
https://stackoverflow.com/questions/65328516
复制相似问题