1

When I run the program, how do I get the list elements in the second loop to print without square brackets around them?

room_lengths=[]
room_widths=[]
areas=[]
print("House floor area calculator")
rooms=int(input("How many rooms are there? "))
a=1
b=1
for x in range(rooms):
    print("How long (m) is Room ",a,"? ")
    length=float(input())
    print("How wide (m) is Room ",a,"? ")
    width=float(input())
    area=length*width
    room_lengths.append(length)
    room_widths.append(width)
    areas.append(area)
    a+=1
print("The total area is calculated as:")
for x in range (rooms):
    print("Room",b)
    ### Below line does not print as desired ###
    print(room_lengths[b-1:b],"x" ,room_widths[b-1:b],"=",areas[b-1:b],"m²")
    b+=1
total_area=sum(areas)
print("The total area is ,",total_area,"m²")
jpp
  • 159,742
  • 34
  • 281
  • 339

2 Answers2

0

Use str.format and extract a single element rather than slicing an array:

print('{0} x {1} = {2} m²'.format(room_lengths[b-1], room_widths[b-1], areas[b-1]))

Example output:

The total area is calculated as:
Room 1
10.0 x 20.0 = 200.0 m²
Room 2
30.0 x 40.0 = 1200.0 m²
jpp
  • 159,742
  • 34
  • 281
  • 339
0
for l, w, a in zip(room_lengths, room_widths, areas):
    print('{0} x {1} = {2} m²'.format(l, w, a))
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88