简体   繁体   English

我不知道这个房间计算器

[英]I can't figure out this room calculator

I need help on getting it to ask the shape of the room, listed below, the floor type they want, listed below, and ask these for how ever many rooms are being calculated.我需要帮助来询问房间的形状,下面列出,他们想要的地板类型,下面列出,并询问这些房间正在计算多少个房间。 Along with printing the price per room, and the total for all rooms.除了打印每个房间的价格,以及所有房间的总价。 The result of running it is below all of the code.运行它的结果在所有代码的下方。

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)

What is the width of the room?: 2 What is the shape of the room (circle, rectangle, regular polygon, trapezoid, triangle)?房间的宽度是多少?:2 房间的形状是什么(圆形、矩形、正多边形、梯形、三角形)? rectangle What is the length the room?: 5 What is the width of the room?: 2 Traceback (most recent call last): File "/Users/••••••/Desktop/coputer coding/Flooring estimator.py", line 100, in print (('$'), rooms(room_count) * floor) TypeError: unsupported operand type(s) for *: 'float' and 'function'矩形 房间的长度是多少?:5 房间的宽度是多少?:2 回溯(最近一次通话最后一次):文件“/Users/••••••/Desktop/计算机编码/Flooring estimator.py” , 第 100 行, in print (('$'), rooms(room_count) * floor) TypeError: unsupported operand type(s) for *: 'float' and 'function'

Your current code had two issues:您当前的代码有两个问题:

1. print (('$'), rooms(room_count) * floor)

Here the floor function is called by writing floor whereas it actually should have been floor()这里的楼层 function 通过写floor来调用,而实际上应该是floor()

2. floor(floorings)

You do not need the floorings parameter since you are taking that as the input from the user.您不需要floorings参数,因为您将其作为用户的输入。

This is the corrected code:这是更正后的代码:

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())

Now here is the execution explanation for the corrected code现在这里是更正代码的执行说明

  1. The user is asked to input the number of rooms he wishes to calculate the results for.要求用户输入他希望计算结果的房间数量。 This value is read in the number_of_rooms variable.该值在number_of_rooms变量中读取。

  2. Then the total cost for the purchase/calculations is found out by summing the individual costs of flooring and total cost for all the rooms which is calculated using the rooms(room_shape) function.然后通过将单独的地板成本和使用rooms(room_shape) function 计算的所有房间的总成本相加,得出购买/计算的总成本。

  3. The rooms(room_shape) function repeats a set of steps for all the rooms. rooms(room_shape) function 对所有房间重复一组步骤。 The steps being calculating the floor area of each room and adding it to the overall floor area which is tracked by using the total_area variable.步骤是计算每个房间的建筑面积并将其添加到使用total_area变量跟踪的整体建筑面积中。

  4. Once the total area for all the rooms is calculated based on the input from user where he/she selects the type of room they wish, the value of total_area is returned.一旦根据用户的输入计算了所有房间的总面积,他/她在其中选择了他们想要的房间类型,就会返回total_area的值。

  5. This is then added to the value returned from the floor variable.然后将其添加到从floor变量返回的值中。 Thus giving you the final cost.从而给你最终的成本。

However this floor cost is added just once and is not at all calculated the way you expect it to be calculated.但是,此底价仅添加一次,并且根本不会按照您期望的计算方式计算。 I suppose to calculate it for each room where each room may have different type of flooring such as我想为每个房间计算它,每个房间可能有不同类型的地板,例如

total_cost = room_area1 * floor1 + room_area2 * floor .... so on

For that you should use the code which looks something like this:为此,您应该使用如下所示的代码:

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))

Here the only change being, floor() is called for each room instead of being called once after calculation of net area.这里唯一的变化是,对于每个房间调用floor() ,而不是在计算净面积后调用一次。

But the current code still has an issue where total and total_count are not used correctly to add and update the net area.但是当前代码仍然存在一个问题,即未正确使用totaltotal_count来添加和更新净面积。 Instead to get the prices of individual rooms you could use the following method:而不是获取单个房间的价格,您可以使用以下方法:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM