Question

In: Computer Science

hi i do not know what is wrong with my python code. this is the class:...

hi i do not know what is wrong with my python code.

this is the class:

class Cuboid:
    def __init__(self, width, length, height, colour):
        self.__width = width
        self.__length = length
        self.__height = height
        self.__colour = colour
        self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height))
        self.volume = height * length * width

    def get_width(self):
        return self.__width

    def get_length(self):
        return self.__length

    def get_height(self):
        return self.__height

    def get_colour(self):
        return self.__colour

    def set_width(self, width):
        if isinstance(width, (int, float)):
            self.__width = width

    def set_length(self, length):
        if isinstance(length, (int, float)):
            self.__length = length

    def set_height(self, height):
        if isinstance(height, (int, float)):
            self.__height = height

    def set_colour(self, colour):
        if colour:
            if colour(" ", "").isalnum():
                self.__colour = colour

    def get_surface_area(self, surface_area):

        return self.surface_area

    def get_volume(self, volume):
        return self.volume

    def __str__(self):
        desc = f"The {self.__colour} Cuboid Measurments: \n" \
               f"(Width = {self.__width}, Length = {self.__length}, Height = {self.__height})" \
               f"Surface Area = {self.surface_area}" \
               f"Volume = {self.volume}"
        return desc

this is the demo code:

from cuboid import Cuboid
def main():
    cuboid1 = Cuboid("Blue", 10, 13, 54)
    cuboid2 = Cuboid("Red", 11, 63, 78)

    print(cuboid1)
    print(cuboid2)

main()

Solutions

Expert Solution

Hi I have resolved the errors.. The code is right but the method signatures were wrong... Like for init you didn't put double underscore.. And a small small syntax errors are there.. Also you have declared the init method with width, length, height, color but in mai you are passing color, height, length, width like that the arguments should be in the same order. Find the below code..

class Cuboid:
def __init__(self, width, length, height, colour):
self.__width = width
self.__length = length
self.__height = height
self.__colour = colour
self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height))
self.volume = height * length * width

def get_width(self):
return self.__width

def get_length(self):
return self.__length

def get_height(self):
return self.__height

def get_colour(self):
return self.__colour

def set_width(self, width):
if isinstance(width, (int, float)):
self.__width = width

def set_length(self, length):
if isinstance(length, (int, float)):
self.__length = length

def set_height(self, height):
if isinstance(height, (int, float)):
self.__height = height

def set_colour(self, colour):
if colour:
if colour(" ", "").isalnum():
self.__colour = colour

def get_surface_area(self, surface_area):

return self.surface_area

def get_volume(self, volume):
return self.volume

def _str_(self):
desc=""
desc = desc+"The "+str(self.__colour)+" Cuboid Measurments:" +"Width = "+str(self.__width) +", Length = "+str(self.__length)+", Height = "+str(self.__height) +" , Surface Area = "+str(self.surface_area) +" , Volume = "+str(self.volume)
return desc


def main():
cuboid1 = Cuboid(10,13,54,"Blue")
cuboid2 = Cuboid(11,63,78,"Red")
print(cuboid1.get_colour())
print(cuboid2.get_colour())
print(cuboid1.get_volume(900))
print(cuboid2.get_surface_area(89))
print(cuboid1._str_())
main()

Output :


Related Solutions

HI can I please know whats wrong in this 2to1 mux code in VHDL code also...
HI can I please know whats wrong in this 2to1 mux code in VHDL code also please type it out so theres no confusion thank you -- Code your design here library IEEE; use IEEE.std_logic_1164.all; -- entity declaration for testbench entity test mux2 is end test; --architecture Body declaration for 2to1 mux -- component declaration of source entity 2to1 mux component test mux2 is port ( sel : in std_logic ; --select input, A : in std_logic ; --data input...
I have an unexpected indent with my python code. please find out whats wrong with my...
I have an unexpected indent with my python code. please find out whats wrong with my code and run it to show that it works here is the code : def main(): lis = inputData() customerType = convertAcct2String(lis[0]) bushCost = getBushCost(lis[0],int(lis[1],10)) flowerCost = getFlowerBedCost(int(lis[2],10),int(lis[3],10)) fertiCost = getFertilCost(int(lis[4],10)) totalCost = calculateBill(bushCost,fertiCost,flowerCost) printReciept(customerType,totalCost,bushCost,fertiCost,flowerCost) def inputData(): account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage = input("Please enter values").split() return [account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage] def convertAcct2String(accountType): if accountType== "P": return "Preferred" elif accountType == "R": return "Regular" elif accountType == "N": return...
What's wrong with my Python code. We have to find the regularexpression in Python My output...
What's wrong with my Python code. We have to find the regularexpression in Python My output should look like this My IP address 128. 0. 0. 1 My IP address 53. 20. 62. 201 My code ipAddresses = [] ipRegEx = re.compile(r"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")    result = myRegEx.find all(Search Text)    def Regular_expression(ipAddresses)       for i in ipAddresses:          print(i) ipSearchResult = ipRegEx.search(line)    if ipSearchResult != None and ipSearchResult.group() not in ipAddresses:       ipAddresses.append(ipSearchResult.group()) we have to use loop too.
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
Python I am creating a class in python. Here is my code below: import csv import...
Python I am creating a class in python. Here is my code below: import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(obj.POP(line)) population.append(obj.POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip() The problem is, I get an error saying:  ...
I do not know why I keep getting this question wrong, I triple checked my work!...
I do not know why I keep getting this question wrong, I triple checked my work! A distribution of values is normal with a mean of 80 and a standard deviation of 18. From this distribution, you are drawing samples of size 23. Find the interval containing the middle-most 40% of sample means: ANSWER HERE Enter your answer using interval notation. In this context, either inclusive or exclusive intervals would be acceptable. Your numbers should be accurate to 1 decimal...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
Can you please see what I have done wrong with my program code and explain, This...
Can you please see what I have done wrong with my program code and explain, This python program is a guess my number program. I can not figure out what I have done wrong. When you enter a letter into the program, its supposed to say "Numbers Only" as a response. I can not seem to figure it out.. instead of an error message. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT