In: Computer Science
Problem IN PYTHON ( Use Fuctions and or with Loop if comfortable) Your friends just bought a new five room house and need your help to know how much it will cost to re-do the floors. Write a program that, given the dimensions of each of the five rooms and the desired type of flooring, outputs the cost of each room as well as the total cost. Hardwood costs $1.39/sqft, carpet costs $3.99/sqft, and tile costs $4.99/sqft. Input Validation: Your program must work regardless of the case the floor option is typed in and any blank spaces surrounding it; If an invalid option is given, inform the user it was invalid and that you will be skipping that room (treat the cost as zero). Round: Use the round function to round your output to two decimal places.
The python program for the above problem is:
def input_data():
length = []
width = []
cost = []
for i in range(5):
print('Enter length of room {} in ft: '.format(i+1), end = "")
l = input()
print('Enter width of room {} in ft: '.format(i+1), end = "")
w = input()
if l.isnumeric() and w.isnumeric():
length.append(float(l))
width.append(float(w))
else: # invalid case was found
print('Invalid option was entered!')
length.append(0)
width.append(0)
print('Enter type of floor of room {}: '.format(i+1), end = "")
t = input()
t = t.strip()
if t.lower() == 'hardwood':
cost.append(1.39)
elif t.lower() == 'carpet':
cost.append(3.99)
elif t.lower() == 'tile':
cost.append(4.99)
else: # invalid case is taken into account
print('Invalid option was entered!')
cost.append(0)
return length, width, cost
length, width, cost = input_data() # function used to input data
total = 0 # total cost for rooms is initialised to 0
for i in range(5):
print('The cost of flooring of room {} is Rs. {}.'.format(i+1, round(length[i]*width[i]*cost[i], 2))) # cost of each room is found
total += length[i]*width[i]*cost[i]
print('The total cost of flooring of all rooms is Rs. {}.'.format(round(total,2))) # total cost of all rooms is found
The sample of the code is:
The output sample of the code is:
The code for the above problem using while loop is:
def input_data():
length = []
width = []
cost = []
i = 0
#for i in range(5):
while i < 5:
print('Enter length of room {} in ft: '.format(i+1), end = "")
l = input()
print('Enter width of room {} in ft: '.format(i+1), end = "")
w = input()
if l.isnumeric() and w.isnumeric():
length.append(float(l))
width.append(float(w))
else: # invalid case was found
print('Invalid option was entered!')
length.append(0)
width.append(0)
print('Enter type of floor of room {}: '.format(i+1), end = "")
t = input()
t = t.strip()
if t.lower() == 'hardwood':
cost.append(1.39)
elif t.lower() == 'carpet':
cost.append(3.99)
elif t.lower() == 'tile':
cost.append(4.99)
else: # invalid case is taken into account
print('Invalid option was entered!')
cost.append(0)
i += 1
return length, width, cost
length, width, cost = input_data() # function used to input data
total = 0 # total cost for rooms is initialised to 0
for i in range(5):
print('The cost of flooring of room {} is Rs. {}.'.format(i+1, round(length[i]*width[i]*cost[i], 2))) # cost of each room is found
total += length[i]*width[i]*cost[i]
print('The total cost of flooring of all rooms is Rs. {}.'.format(round(total,2))) # total cost of all rooms is found
Comment down if you have any queries regarding the code. I will help you out as soon as possible.