简体   繁体   中英

Missing Positional Arguments

from time import sleep 


def Hotelreception():
    print("★ Hotel ★ \n")
    sleep(0.05)
    print("★ 1 Create Booking ★ \n")
    sleep(0.05)
    print("★ 2 Cancel Booking ★ \n")
    sleep(0.05)
    print("★ 3 Display Bills  ★ \n")
    first_input = int(input("Please enter your choice:\n"))

    if first_input == 1: 
        Create_Booking(1, 2, 3)    #3 required positional arguments here, what is in the () does not matter, because it gets overwritten.

    elif first_input == 2:
        pass
    
    elif first_input == 3:
        pass

class Create_Booking():
    def __init__(self, name, phonenr, adress):
        self.name = name
        self.phonenr = phonenr
        self.adress = adress

        name = []
        phonenr = []
        adress = []

        checkin_name = input("Enter a Name:\n")
        checkin_phonenr = input("Enter a Phone Number:\n") #This can be a string, as I do not need to calculate with this.
        checkin_adress = input("Enter an Adress:\n")

        if checkin_name == "" and checkin_phonenr == "" and checkin_adress == "":    #This checks if any neccesary inputs have been left out.
            print("You haven't entered anything.")

        elif checkin_name == "" and checkin_phonenr == "":    
            print("Name and Phone Number is empty.")

        elif checkin_phonenr == "" and checkin_adress == "":
            print("Phone Number and Adress is empty.")

        elif checkin_name == "" and checkin_adress == "":
            print("Name and Adress is empty.")
    
        elif checkin_name == "":   
            print("Name is empty.")

        elif checkin_phonenr == "":
            print("Phone Number is empty.")

        elif checkin_adress == "":
            print("Adress is empty.")

        else:
            name.append(checkin_name)
            phonenr.append(checkin_phonenr)
            adress.append(checkin_adress)
            print(name, adress, phonenr)
    

My Question is why do I absolutely need three positional Arguments with the function Create_Booking(), while the function Hotelreception() can be empty and left without any arguments? What do these arguments do, because if I enter Create_Booking(1, 2, 3) it suddenly works.

Create_Booking is a class and not a function (see class keyword vs def keyword). The class Create_Booking will look to it's init function when a new instance of that class is initialized. The init in this case takes three arguments

def __init__(self, name, phonenr, adress):

Which are all non-optional (positional) arguments.

Some resources: https://www.learnpython.org/en/Classes_and_Objects https://www.geeksforgeeks.org/default-arguments-in-python/

Hotelreception is a function that takes no arguments:

def Hotelreception():

Create_Booking is a class , with a constructor that takes three arguments:

class Create_Booking():
    def __init__(self, name, phonenr, adress):

When you execute Create_Booking(1, 2, 3) , you are calling the constructor. The parameter values are assigned to the name , phonenr , and adress parameters of the constructor.

You have specifically specified that Create_Booking have three arguments. the def __init__(self): initializes the class. As you have initialized the class with the requirement of three parameters def __init__(self, name, phonenr, adress): it requires three parameters to run. If you only want these parameters to be optional you can set a default value by including the equal sign and the default value, such as def __init__(self, name = 'john', phonenr = '139030303', adress = '12 street'): . If you want it to run without any inputs or default values, remove name , phonenr , and adress .

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