繁体   English   中英

python类,def __init __():和输入语句

[英]python classes, def __init__(): and input statements

我正在尝试定义一个名为“度假”的类,以便在用户回答了一组输入语句后存储有关用户的信息(我的项目是一个度假程序,其中用户回答了一组问题,以便接收随机生成的度假路线)。 我的教授告诉我将我所有的问题(也称为输入语句)放在构造函数中,但是我不确定我为什么会出现错误,说我有语法错误。 我试过将不同的变量名放在构造函数的()中,但这没有任何作用(我认为我可能有语法错误,因为我忘记了包含其中一个变量名-特别是错误来自“游览”)。 我刚接触Python(刚从一月份开始),所以我对编程语言的了解非常有限(加上大约两周前我刚刚学习了课程)。 任何和所有帮助/反馈/想法将不胜感激!

class Vacation:                  
    def __init__ (self, num_guests, max_price, vacay_len, excursions, user_input, excursions_price, excursions_num, user_preference):
        user_input = input("Would you like a (R)andomly generated vacation or a (T)ailored vacation?: ")
        num_guests = int(input("Please enter number of guests: "))
        max_price = float(input("Please enter your maximum willingness to pay (ie. 700): "))
        vacay_len = int(input("Please enter how many days you would like your vacation to be: ")
        excursions = (input("Would you like to include excursions while on vacation? (Y/N) : ")).upper()
        if excursions == "Y":
            excursions_num = int(input("How many excursions would you like to sign up for (up to 2): "))
            excursions_price = float(input("How much would you like to spend on each excursion? : "))
        else:
            print("Let's get to exploring!")
        user_preference = user_input
        self.user_preference = user_preference
        self.num_guests = num_guests #number of guests
        self.max_price = max_price #maximum price range
        self.housing_price = .3(self.max_price) #housing budget allocation 
        self.vacay_len = vacay_len #vacation length
        self.excursions_price = excursion_price #price for each excursion  
        self.excursions_num = excursions_num #number of excursions

您在此行上缺少右括号:

vacay_len = int(input("Please enter how many days you would like your vacation to be: "))

您有错别字excursion_price应该是excursions_price ,如下所示:

self.excursions_price = excursions_price #price for each excursion

您还需要在此处添加*进行乘法运算:

self.housing_price = .3*(self.max_price)

在这种情况下,您可以从__init__函数中删除变量,因为它们将由程序的用户输入提供。

我建议使用python IDE,它将以红色突出显示您的问题。

不知道这是否是您想要的,但是以下代码似乎可以工作:

class Vacation:
    def __init__ (self):
        user_input = input("Would you like a (R)andomly generated vacation or a (T)ailored vacation?: ")
        num_guests = int(input("Please enter number of guests: "))
        max_price = float(input("Please enter your maximum willingness to pay (ie. 700): "))
        vacay_len = int(input("Please enter how many days you would like your vacation to be: "))
        excursions = (input("Would you like to include excursions while on vacation? (Y/N) : ")).upper()

        if excursions == "Y":
             excursions_num = int(input("How many excursions would you like to sign up for (up to 2): "))
             excursions_price = float(input("How much would you like to spend on each excursion? : "))
        else:
            print("Let's get to exploring!")
        user_preference = user_input
        self.user_preference = user_preference
        self.num_guests = num_guests #number of guests
        self.max_price = max_price #maximum price range
        self.housing_price = .3(self.max_price) #housing budget allocation
        self.vacay_len = vacay_len #vacation length
        self.excursions_price = excursion_price #price for each excursion
        self.excursions_num = excursions_num #number of excursions

正如史蒂夫建议的那样,由于您可能不会传递参数,因此我从构造函数参数中删除了变量。 在游览行中,您只是错过了结尾)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM