I'm new to Python and taking a class for it. I'm stuck on one part of my homework, where I have to create a table that calculates the volume (length x width x height) of all rectangular shapes where the length, width, and heights must be between 1 and 5. So the table should be like this:
Length | Width | Height | Volume |
---|---|---|---|
1 | 1 | 1 | 1 |
1 | 1 | 2 | 2 |
1 | 1 | 5 | 5 |
1 | 2 | 1 | 2 |
1 | 2 | 2 | 4 |
1 | 2 | 3 | 6 |
… | … | … | … |
I have tried this but it got me an error.
length = range (1,5)
width = range (1,5)
height = range (1,5)
volume = length * width * height
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
volume = length * width * height
TypeError: unsupported operand type(s) for *: 'range' and 'range'
I also tried a zip command I saw online but it didn't help much either. I guess how would I start the code?
length = [1, 2, 3, 4, 5]
width = [1, 2, 3, 4, 5]
height = [1, 2, 3, 4, 5]
volume = [i * j * n for i, j, n in zip(length, width, height)]
print("length", "width", "height", "volume\n", length, width, height, volume)
length width height volume
[1, 2, 3, 4, 5] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] [1, 8, 27, 64, 125]
#This doesn't do all of the combinations like I wanted