首页 > 解决方案 > "TypeError: product_show() missing 1 required positional argument: 'Option'"

问题描述

I have build up a menu with numbers, and my selection variable is Option with type int. 5-Hoodie will show all the stuff with category is Hoodie in my SQL database. I have created a module and I am using the class named LunaPy

Option = int(input("Option: "))
LunaPy.product_show(Option)

I am using SQL library in Python

    def product_show(self,Option):
        product_dict ={1:"Belt",2:"Blazer",3:"Coat",4:"Dress",5:"Hoodie",6:"Jacket",7:"Jeans",8:"Pants",9:"Shoes",10:"Shorts",11:"Sunglasses",12:"Sweater",13:"SweatShirt",14:"TShirt",15:"Underwear"}

        query = "SELECT * FROM LunaPyDataBase WHERE Category = ?"
        self.cursor.execute(query,(product_dict[Option],))

I expected the Option variable would return the value to the function so the function can use that to select the category in dictionary. And prints the items in that chosen category.

标签: python-3.xsqlite

解决方案


Change your method to this:

@staticmethod
def product_show(Option):
    product_dict ={1:"Belt",2:"Blazer",3:"Coat",4:"Dress",5:"Hoodie",6:"Jacket",7:"Jeans",8:"Pants",9:"Shoes",10:"Shorts",11:"Sunglasses",12:"Sweater",13:"SweatShirt",14:"TShirt",15:"Underwear"}

    query = "SELECT * FROM LunaPyDataBase WHERE Category = ?"
    self.cursor.execute(query,(product_dict[Option],))

Or do this:

option = int(input("Option: "))
lunaPy = LunaPy()

lunaPy.product_show(option)

The self in your function definition points to the object instance of your LunaPy class. If your method does not require an instance of LunaPy, you can mark it as static... and then you can use it like this Class.method(), but won't be able to use any instance variables or methods of the class.

The other option is to just create the instance and call the method using that.

EDIT:

Didn't notice the self inside the function. The first option won't work, because object instance is required. The second option with lunaPy = LunaPy() should work though.


推荐阅读