计算总楼层尺寸(Python)

2024-09-30 01:22:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要计算一下总的楼层面积。它首先询问房间的数量(所有房间都是矩形的),然后使用for循环询问每个房间的长度和宽度。以下是我的代码(python):

norooms = int(input('How many rooms does the floor have?'))
length = dict()
width = dict()
for i in range(norooms):
    length[i] = input('what is the length of room?')
    width[i] =input('what is the width of room?')
    print('that was room number:',i+1)

现在我得计算一下地板的总数。我能帮个忙吗?在


Tags: oftheforinput数量iswidthwhat
3条回答
norooms = int(input('How many rooms does the floor have?'))
length = dict()
width = dict()
for i in range(norooms):
    length[i] = input('what is the length of room?')
    width[i] =input('what is the width of room?')
    print('that was room number:',i+1)
total=0
for i in range(norooms):
    total += float(length[i])*float(width[i])

print 'total is:' ,total

如果它在Programming Exchange中,我建议改为使用以下代码:

^{pr2}$

使用numpy可以很快解决这个问题。如果你将来要做类似的事情,花点时间学习图书馆也许是值得的。在

不管怎样,给你:

import numpy as np

no_rooms = int(input('How many rooms does the floor have?'))

length = np.empty(no_rooms)
width = np.empty(no_rooms)

for i in range(no_rooms):
    length[i] = input('what is the length of room?')
    width[i] = input('what is the width of room?')
    print('that was room number:', i+1)

print('total is: ' + str(np.dot(length, width)))

你知道怎样计算给定房间的面积?对所有房间都这样做,然后计算总数。还要注意,目前房间的长度和宽度仍然是字符串。在

相关问题 更多 >

    热门问题