代碼:如何獲取一個函數中的變量到python中的另一個函數?
def create_Table():
c.execute('CREATE TABLE IF NOT EXISTS customer(name TEXT NOT NULL, accountNo INTEGER NOT NULL, balance INTEGER NOT NULL)')
conn.commit()
def data_insert():
name = str(input("Enter your name: "))
accountNo = random.randrange(2016000,2025000)
balance = int(input("Enter your initial deposit: $"))
if balance>0:
print("You have successfully opened an account, your account number is: ",accountNo)
else:
print("Incorrect initial deposit, Please deposit $1 or more")
c.execute("INSERT INTO customer VALUES(?, ?, ?)",(name, accountNo, balance))
def authentication():
user_Id_Input = int(input("Enter your account number: "))
c.execute("SELECT * FROM customer WHERE accountNo = ?",(user_Id_Input,))
return user_Id_Input
conn.commit()
def user_balance():
authentication()
if c.fetchall() is not None:
c.execute("SELECT balance FROM customer WHERE accountNo = ? "(user_Id_Input,))
data = c.fetchone()
print("Your balance is: $",data)
else:
print("You have entered an incorrect account number.")
conn.commit()
我想要得到的變量user_Id_Input
從def authentication():
到def user_balance():
。即使我編碼authentication()
在def user_balance():
我仍然無法獲得該變量的balance()
函數。
您正在從'authentication()'返回'user_Id_Input',但未捕獲'user_balance()'中返回的值。在'user_balance()'中使用'user_Id_Input = authentication()'。你也應該在'authenticate()'''return'語句之前移動'conn.commit()' – kuro