我怎样才能创建主内部课程? [英] How can I create main inside class?

查看:103
本文介绍了我怎样才能创建主内部课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习使用类中的main函数,但因为我不知道如何使用它所以我的代码没有给出任何输出。据我所知,我需要在__init__方法下定义def main来写name == main,但问题是我不知道如何在主函数内部编写def getRoom和其他函数。我可以得到一些建议如何编写正确的程序?



我的代码如下:



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")





我尝试过:



我试图创建def main()但这对我的def getRoom不起作用和

def getCatagory



What I have tried:

I have tried to create " def main()" but that did not work with my "def getRoom" and
"def getCatagory"

推荐答案

你应该(自己)问'如何',但'为什么' Python不是Java [ ^ ]。
You should ask (yourself) not 'how' but 'why'. Python is not Java[^].


你应该去 Python教程 - Python 3.7.2文档 [ ^ ]并通过它解决所有问题。
You should go to The Python Tutorial — Python 3.7.2 documentation[^] and work through it, where all your issues will be explained.


这篇关于我怎样才能创建主内部课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆