python-3.x - "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.
解决方案
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.
推荐阅读
- python - wget 和 requests.get 在文件下载方面的区别
- amazon-web-services - Terraform:创建和验证多个 ACM 证书
- javascript - 如何使 ng-change 工作以将句子 1 更改为句子 2
- sql - 错误 979:不是 GROUP BY 表达式
- canvas - createJS 坐标与画布不匹配
- jquery - 多个 Select2 下拉菜单的不同图标
- android - Android 版本检查问题(已弃用)
- vue.js - Vue :click 在 v-for 中自动触发
- c# - 在 Unity 上显示 Google API 地图
- html - CSS动画,播放然后在悬停时反转动画