Home > database >  How can I test my function before storing my input into a database or json file?
How can I test my function before storing my input into a database or json file?

Time:11-15

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. Eventually, I want to be able to use a sqlite database to hold my data. I have not coded the database functionality yet, but I wanted to test my function to at least see if the logic works. 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?

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:

NameError: name 'initiate_habit' is not defined `

CodePudding user response:

The method, is an instance method, how could the code know initiate_habit is related to habit if you don't tell it

Shoule be

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