简体   繁体   English

在将我的输入存储到数据库或 json 文件之前,如何测试我的 function?

[英]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_habithabit有关

Shoule be应该是

habit = Habit('Read', 'Read 15 pages daily')
habit.initiate_habit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何运行我的脚本远程存储数据库中的数据? - How can I run my script remotely storing data in database? 我可以在测试用例中模拟功能输入吗? - Can I emulate input for my function inside the Test Case? 如何将我的 Firestore 数据库从 Google 存储桶导出到 Json 文件中 - How can I export my Firestore database from a Google Storage Bucket into a Json file Discord.py 如何通过命令在我的 json 文件/数据库中给每个人钱? - Discord.py How can I give everyone money in my json file/database trough a command? 如何让我的输入打开一个数据文件,然后通过 function 使其成为 go? - How can I let my input open a data file and then make it go through the function? 如何将 PyMongo insert_one function 与 json 文件一起使用,但使用我自己的 _id? - How can I use PyMongo insert_one function with a json file but with my own _id? 如果将db连接存储在flask.g对象上,如何使Flask连接到测试数据库? - How do I get Flask to connect to my test database if I'm storing the db connection on the flask.g object? 如何使用部分文件名作为函数的输入? - How do I use part of my file names as input for a function? 如何使函数接受不同类型的输入? - How can I make my function accept different types of input? 我的 function 正在更改输入变量,如何防止或解决此问题? - My function is changing an input variable how can I prevent or fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM