這是以下函式的代碼
功能:
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)
錯誤代碼
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")
完整代碼:https ://srcb.in/l1RdtphmhF
這是代碼是餐廳資料庫管理系統。我正在借助 mysql 制作這個系統。所有函式都作業正常,但是當我呼叫 dishID 函式時,它會產生一個無法讀取該函式的錯誤。準確地說,我希望代碼能夠作業,以便它可以插入一些值
uj5u.com熱心網友回復:
好的,我查看了您的鏈接,問題非常簡單;dishID
您僅在實際呼叫函式后才定義該函式。這是此問題的一個簡單示例 - 以下是一些運行良好的代碼:
def test_function():
print('hi')
test_function()
Output: hi
與此代碼相比,它test_function
在其定義之前參考:
test_function()
def test_function():
print('hi')
Output: NameError: name 'test_function' is not defined
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/537597.html
上一篇:C-函式的單一定義規則