[英]How can I test my function before storing my input into a database or json file?
Perhaps someone here can help me.也许这里有人可以帮助我。 I am trying to create a habit-tracking app as a project and I have created a habit class along with a habit creation function that I defined.
我正在尝试创建一个习惯跟踪应用程序作为一个项目,并且我已经创建了一个习惯 class 以及我定义的一个习惯创建 function。 Eventually, I want to be able to use a sqlite database to hold my data.
最终,我希望能够使用 sqlite 数据库来保存我的数据。 I have not coded the database functionality yet, but I wanted to test my function to at least see if the logic works.
我还没有对数据库功能进行编码,但我想测试我的 function 以至少看看逻辑是否有效。 Until now, this is what I have:
到现在为止,这就是我所拥有的:
from datetime import date
class Habit:
def __init__(self, name: str, description: str):
self.name = name
self.description = description
def initiate_habit(self):
habit_name = input('Enter a habit name: ')
type = input('Enter a habit type: ')
duration = input("Enter habit duration (daily, weekly, monthly): ")
start_date = date.today()
end_date = input('Enter end date: ')
When I try to call my function, I get the following error: NameError: name 'initiate_habit' is not defined Can someone tell me where I'm going wrong?当我尝试拨打我的 function 时,出现以下错误: NameError: name 'initiate_habit' is not defined 谁能告诉我哪里出错了?
To test:去测试:
habit = Habit('Read', 'Read 15 pages daily')
initiate_habit()
When I try running my initiate_habit function, I receive the below-mentioned error:当我尝试运行我的 initiate_habit function 时,我收到以下错误:
NameError: name 'initiate_habit' is not defined `
NameError:名称“initiate_habit”未定义`
The method, is an instance method, how could the code know initiate_habit
is related to habit
if you don't tell it method,是一个实例方法,不告诉代码怎么知道
initiate_habit
和habit
有关
Shoule be应该是
habit = Habit('Read', 'Read 15 pages daily')
habit.initiate_habit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.