Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to learn using main function inside a class but as I am not sure how to use it so my code is not giving any output. As far as i learned I need to define def main under the __init__ method to write name == main, but the problem is I do not know how to write the "def getRoom" and other function inside that main function. Can i get some suggestion how can I write the correct program?

My code is given below:

class Hotel:
    def __init__(self,room,catagory):
        
        if type(catagory) != str:
            raise TypeError()
        if room<0 or room >5:
            raise ValueError()
        self.room = room
        self.catagory = catagory
        self.catagories = {"A":"Elite","B":"Economy","C":"Regular"}
        self.rooms = ["0","1","2","3","4","5"]
        

    def getRoom(self):
        return self.room

    def getCatagory(self):
        
        return self.catagories.get(self.catagory)
    def __str__(self):
        return "%s and %s"%(self.rooms[self.room],self.catagories.get(self.catagory))
if (__name__== "__ main__"):
    r = Hotel ()
    print (r)
    try:
        room1 = Hotel(2,1)
        print (room1)
    except TypeError: 
        print("Error")
    try:
        room2= Hotel(1,"A")
        print (room2)
    except ValueError:
        print ("mistake")


What I have tried:

I have tried to create " def main()" but that did not work with my "def getRoom" and
"def getCatagory"
Posted
Updated 18-Feb-19 21:54pm

You should ask (yourself) not 'how' but 'why'. Python is not Java[^].
 
Share this answer
 
You should go to The Python Tutorial — Python 3.7.2 documentation[^] and work through it, where all your issues will be explained.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900