Can someone explain to me why this code doesn't work?

import random

height = [2, 2.34, 3, 3.45, 4, 6, 10.2]
width = [15, 100, 6.67, 3.99, 5.67, 2.67, 200]

select a program that removes 2 values from this list

random_height = random.sample(height, 1)
random_width = random.sample(width, 1)

convert into square meters

square_meters = random_height * random_width

divide by the total area of the paint

paint_quantity = square_meters / 2

print(f’With a wall of {random_height:.2f} height x {random_width:.2f} width\nYou will need {quantity_paint:.2f} of paint buckets.')

TypeError: can’t multiply sequence by non-int of type ‘list’

If you ask me what’s wrong I don’t know how to answer. I’ve been learning programming for 7 days and I barely know what I’m doing

3 Likes

can you give me the link name?

Where the code says

Did you try to check what random_height looks like after that?
What do you think should be in random_height after that?
When you check it, is it as you expect?
What does the documentation tell you about random.sample?

2 Likes

1 Like

ok my friend, I already corrected what you helped me with. But now I’m having a new problem at 1 hour I can’t solve it:

msg error:

88, in
square_meters = new_height * new_width
~^~~~~
TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘NoneType’

the code looked like this

import random

height = [2, 2.34, 3, 3.45, 4, 6, 10.2]
width = [15, 100, 6.67, 3.99, 5.67, 2.67, 200]

#random heights
new_height = print(random.sample(height, 1))
new_width = print(random.sample(width, 1))

convert into square meters

square_meters = new_height * new_width

total ink

paint_quantity = square_meters / 2

print(f’Wall is a wall of {square_meters}\nYou will need {quantity_paint:.2f} of paint cans.')

You need to look carefully at the code that you write. There are three errors here. I will give you a couple of clues of where to look:

1. The first is that quantity_paint is not the same as paint_quantity.
2. The second is that in your code random_height and random_width are not numbers but lists with one item. You need to change this.