简体   繁体   中英

python classes, def __init__(): and input statements

I am trying to define a class called 'Vacation' to store information about the user once they have answered a set of input statements (my project is a vacation program where the user answers a set of questions in order to receive a randomly generated vacation itinerary). My professor told me to put all of my questions (aka input statements) within my constructor, however I keep getting an error saying I have a syntax error although I am not sure why. I have tried putting the different variable names in the () of my constructor but this does nothing (I thought maybe I had a syntax error because I forgot to include one of the variable names - specifically the error is coming from 'excursions'). I am fairly new to Python (just started in January) so my knowledge of the programming language is very limited (plus I just learned classes about 2 weeks ago). Any and all help/feedback/ideas would be greatly appreciated!

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

You are missing closing bracket on this line:

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

You have a typo excursion_price should be excursions_price as below:

self.excursions_price = excursions_price #price for each excursion

You also need to add * for multiplication here:

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

You can remove the variables from the __init__ function in this case since they will be provided by the users input to the programme.

I would recommend using a python IDE which will highlight the issues for you in red.

Not sure if this is what you want but the following code seems to work:

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

As suggested by Steve I removed your variables from the constructors parameters as you probably won't be passing arguments. At the Excursions line you were simply missing a closing )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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