Home > Blockchain >  NameError: name 'dishID' is not defined. Did you mean: 'dishid'?
NameError: name 'dishID' is not defined. Did you mean: 'dishid'?

Time:11-19

this is the code of the following function

Function:

def dishID():
    query = 'select count(*), max(DishID) from Dish'
    cur.execute(query)
    fetch = cur.fetchall()
    for i in fetch:
        if i[0] == 0:
            return 1
        else:
            return (int(i[1])   1)

Error code

dishname = input('Enter Dish Name: ')
        dishprice = input('Enter Dish Price: ')
        dishid = str(dishID())
        query = 'insert into Dish values({}, {}, {})'.format(
            dishname, dishprice, dishid)
        cur.execute(query)
        con.commit()
        print("Dish has added successfully")

Full code: https://srcb.in/l1RdtphmhF

This is code is restaurant Database management system. I am taking the help of mysql and making this system. all the functions work fine but when i call the dishID function it produces an error where it cant read the function. To be precise i want the code to work so it can insert some values

CodePudding user response:

Okay I looked at your link and the issue is extremely simple; you defined the function dishID only after you actually call it. Here's a simple example of this issue - here's some code that works fine:

def test_function():
    print('hi')

test_function()
Output: hi

Versus this code, which references test_function before its definition:

test_function()

def test_function():
    print('hi')
Output: NameError: name 'test_function' is not defined
  • Related