Question

In: Computer Science

Problem IN PYTHON ( Use Fuctions and or with Loop if comfortable) Your friends just bought...

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.


Solutions

Expert Solution

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.


Related Solutions

IN PYTHON ( Use Fuctions and or with Loop if comfortable) Your friends just bought a...
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...
Python Write a for loop with a range function and format output as currency Use an...
Python Write a for loop with a range function and format output as currency Use an input statement to ask the user for # of iterations using the prompt: #? [space after the ?] & assign to a variable Convert the variable to an integer Use a for loop and a range function to display all whole numbers from 1 to the user entered number Display the value of the item variable on screen in each iteration in the following...
MATLAB: Create your own problem in words that use loop and selection statements in the same...
MATLAB: Create your own problem in words that use loop and selection statements in the same problem. Create a problem that has a somewhat practical, real-life application like entering your PIN code at an ATM, and then after 4 incorrect tries, you are locked out of the ATM. Or create a problem based perhaps upon a physics or chemistry lab.   The problem should require the use of a WHILE loop, a FOR loop and a minimum of 3 selection statements...
You bought a book, a winter hat, and a cupcake for Christmas gifts for your friends....
You bought a book, a winter hat, and a cupcake for Christmas gifts for your friends. There are seven friends (Danise, Caitlin, Matt, Stephen, Christy, Kyoungmin, Juna) among whom you will randomly choose three friends and give each of them Christmas gift. a. What is the probability that Danise, Christy, and Juna will receive a book, a cupcake, and a winter hat, respectively? b. What is the probability that Danise, Chirsty, and Juna will receive Christmas gift from you?
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate...
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate through each of the elements in the temperature list. - Convert each element of this list to a Celsius temperature and then, for each valid temperature in the list, print out both the original Fahrenheit temperature and the Celsius equivalent in this format: "32 degrees Fahrenheit is equivalent with 0 degrees Celsius."
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Use python Only can use if statement and loop, that's professor's requirement I have no idea...
Use python Only can use if statement and loop, that's professor's requirement I have no idea how to do that without class Building Entrance Tracker Due to Covid-19, we can allow only 5 guests to our building at a time. Our task is to write a program that helps the entrance staff to keep track of guest entries to the building. Feature 1 (40 points): Each guest must sign in upon entry into the building. If the number of guests...
must use python Write a nested for loop that displays the following output using the same...
must use python Write a nested for loop that displays the following output using the same integer from part a:                                                     1                                                 1   2   1                                             1   2   4   2   1                                         1   2   4   8   4   2   1                                     1   2   4   8 16   8   4   2   1                                 1   2   4   8 16 32 16   8   4   2   1                             1   2   4   8 16 32 64 32 16   8   4   2   1                         1   2   4  ...
Python Review in class Worksheet You are working with your friends, El, Mike, Will, Lucas and...
Python Review in class Worksheet You are working with your friends, El, Mike, Will, Lucas and Dustin. You are watching the lights in Will’s house flicker on and off and you notice patterns. All of you think it’s a code. So you count the repeating set of flickers, but sometimes you miss the dark count. 38 seconds, light stays steady, 95 seconds light stays steady, Dark for 19 seconds The flickers change 60 seconds flickers, light stays steady, 160 seconds...
To detox or not isn't just a topic of discussion for you and your friends. It's...
To detox or not isn't just a topic of discussion for you and your friends. It's one that is debated regularly among health care practitioners. Detoxification has been practiced for centuries by many cultures around the world. Research a body detoxification method and whether you agree with the practice or not
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT