Home > Software design >  Can someone explain to me how to resolve the argument error below with the use of my code?
Can someone explain to me how to resolve the argument error below with the use of my code?

Time:12-14

The error here is on positional argument error where the code can not execute because of the position argument error on this line of code 'Customer_Cars_1 = Customer1.requestCar()'. I got an error of "TypeError: rentCarOnHourlyBasis() missing 1 required positional argument: 'n' " which I am not sure if It has been resolved. I need an explicit explanation on how to resolve all the errors listed above

THE ERROR:

     Traceback (most recent call last):
      File "c:/Users/CRIME ALERT 3/Documents/project for python/car_rent2.py", line 79, in 
<module>
    CarRtl.rentalPackages(Customer_Cars_1)
TypeError: rentalPackages() takes 1 positional argument but 2 were given

MY CODE

class CarRental:   
    def __init__(self, stock):
    
        self.stock = stock

    def displayStock(self):
    
        print(f"Welcome To Ayodeji Autos.... \n \n \nWe have currently {self.stock} cars 
              available to rent\n \n \n")
        return self.stock
 
    def rentalPackages(self):
    
        numCars = CarRental(int(n))
        numCars.rentalPackages
    
        option = int(input('Ayodeji Autos Rental Packages \nOptions: \n 1 - Hourly 
        Basis\n**************************************\nHow long do you want to rent a car: '))
    
        try:
            option = int(option)
        except ValueError:
            print("That's not a positive integer!")
            return 1
    
        if option == 1:
            CarRental.rentCarOnHourlyBasis(numCars) 
    
        else:
            return CarRental.displayStock()

      
def rentCarOnHourlyBasis(self, n):
    
    if n <= 0:
        print('Number of cars should be positive!')
        
    elif n > self.stock:
        print(f'Sorry! We have currently {self.stock} bikes available to rent.')
        return None
    
    else:
        now = datetime.datetime.now()
        print(f'You have rented a {n} car(s) on hourly basis today at {now.hour} hours.')
        print("You will be charged $5 for each hour per car.")
        print("We hope that you enjoy our service.")
        
        self.stock -= n
        return now
    

class Customer:

    def requestCar(self):
                  
        cars = input("How many cars would you like to rent?")
    
        try:
            cars = int(cars)
        except ValueError:
            print("That's not a positive integer!")
            return 1
        if cars < 1:
            print(f"{cars} is an Invalid input. Number of cars should be greater than zero!")
            return 1
        else:
            self.cars = cars
        return self.cars

if __name__ == '__main__':
    stock = 10
    CarRtl = CarRental(stock)
    CarRtl.displayStock()     
    Customer1 = Customer()
    Customer_Cars_1 = Customer1.requestCar()
    CarRtl.rentalPackages(Customer_Cars_1)

CodePudding user response:

You should add another positional argument as the first argument that is passed in a method is an instance of the object.

CodePudding user response:

CarRtl.rentalPackages(Customer_Cars_1) in this line of code, you are passing 2 arguments to rentalPackages. First one is the instance of the object (whenever we call a method of any class on the object of that class, the instance of that object is get automatically passed as an argument in that method. In this case the object is CarRtl), and the second argument is Customer_Cars_1.

But, In the class CarRental the function rentalPackages(self) takes only one argument that is self where, self represents the instance of object.

If you want to pass another argument in that function you should add one more argument while defining the function. Like this, rentalPackages(self, AnotherArgumentVariableName)

  • Related