首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >我弄不懂这个房间计算器

我弄不懂这个房间计算器
EN

Stack Overflow用户
提问于 2020-12-16 17:50:02
回答 1查看 70关注 0票数 0

我需要帮助让它问房间的形状,下面列出的房间,他们想要的楼层类型,下面列出,并询问这些有多少房间正在计算。同时打印每个房间的价格,以及所有房间的总价格。运行它的结果低于所有代码。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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‘不支持的操作数类型

EN

回答 1

Stack Overflow用户

发布于 2020-12-16 18:19:05

您当前的代码有两个问题:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1. print (('$'), rooms(room_count) * floor)

这里的floor函数是通过编写floor调用的,而它实际上应该是floor()

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
2. floor(floorings)

您不需要floorings参数,因为您将该参数作为来自用户的输入。

这是更正后的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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())

下面是更正后的代码的执行说明

  1. 要求用户输入他想要计算结果的房间数。该值在number_of_rooms变量中读取。

  1. 然后,采购/计算的总成本是通过将flooring的单个成本与所有房间的总成本相加得出的,这是使用rooms(room_shape)函数计算得出的。

  1. rooms(room_shape)函数对所有房间重复一组步骤。步骤是计算每个房间的楼层面积,并将其添加到使用total_area变量跟踪的总楼层面积中。

  1. 根据用户的输入计算出所有房间的总面积后,将返回total_area的值。

  1. 然后将其添加到从floor变量返回的值中。从而为您提供最终成本。

然而,这个地板成本只添加了一次,并且根本不是按照您期望的计算方式计算的。我建议为每个房间计算它,每个房间可能有不同类型的地板,例如

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
total_cost = room_area1 * floor1 + room_area2 * floor .... so on

为此,您应该使用如下所示的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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(),而不是在计算净面积后调用一次。

但是当前的代码仍然存在一个问题,即不能正确使用totaltotal_count来添加和更新网络区域。相反,要获得单个房间的价格,您可以使用以下方法:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65328516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文