I am writing a simple banking ussd code and am getting this error when I try the running the ussd.
* Serving Flask app 'trevussd2' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Running on http://91b657bcef8c.ngrok.io
Traceback (most recent call last):
, line 2070,
``in wsgi_app
response = self.full_dispatch_request()
, line 1515, `
``in full_dispatch_request
rv = self.handle_user_exception(e)
line 1513, in full_dispatch_request
rv = self.dispatch_request()
, line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
, line 147, in ``enterdetails
`` response=user.select(AccountNo)
``UnboundLocalError: local variable 'AccountNo' referenced before assignment
``127.0.0.1 - - [18/Aug/2021 11:10:04] "POST / HTTP/1.1" 500 -
``127.0.0.1 - - [18/Aug/2021 11:10:04] "GET / HTTP/1.1" 200 -
``127.0.0.1 - - [18/Aug/2021 11:10:09] "GET /favicon.ico HTTP/1.1" 404 -
Here is the code sample:
from flask import Flask, request
from flask_ngrok import run_with_ngrok
app = Flask(__name__)
run_with_ngrok(app)
import pymysql
class User():
def connection(self):
connection = pymysql.connect(host="localhost", user="root", password="", db="bankussd", port=3306)
return connection
def select(self,accountno):
user=User()
dbcon = user.connection()
select = "select AccountNo,Nameofclient,Age,Gender,Phonenumber,Email,Balance FROM bankuserst WHERE AccountNo=%s "
cursor = dbcon.cursor()
try:
cursor.execute(select, accountno)
row = list(cursor.fetchall)
return row
except Exception as e:
print("exception", e)
return 0
def insert(self,Nameofclient, Age, Gender, Phonenumber, Email, Balance):
dbcon = self.connection()
cursor = dbcon.cursor()
insert_str = "insert into bankuserst(Nameofclient,Age,Gender,Phonenumber,Email,Balance) values(%s,%s,%s,%s,%s,%s)"
try:
cursor.execute(insert_str, (Nameofclient, Age, Gender, Phonenumber, Email,Balance))
dbcon.commit()
print("data saved successfully")
except Exception as e:
print("insert exception", e)
return 0
dbcon.close()
def show_details(self):
print("Personal details")
print("")
print("Name: ", self.name)
print("Age: ", self.age)
print("Gender: ", self.gender)
print("Phone number: ", self.phonenumber)
print("Email: ", self.email)
return 0
user=User()
class Bank():
def deposit(self, Balance):
amount =Balance+0
# once a custo
user = User()
user.insert(Nameofclient='', Age="", Gender="", Phonenumber="", Email="",Balance=amount)
print("Account balance has been updated: ksh.", Balance)
return 0
def withdraw(self, Balance):
amount =Balance-0
user = User()
user.insert(Nameofclient='',Age='',Gender='',Phonenumber='',Email='',Balance=amount)
print("New account balance is : ksh.", Balance)
return 0
def view_balance(self):
self.show_details()
print("Account balance: ", self.balance)
return 0
bank=Bank()
@app.route('/', methods=("POST", "GET"))
def enterdetails():
global response
session_id = request.values.get("sessionId", None)
service_code = request.values.get("sessionCode", None)
phone_number = request.values.get("phonenumber", None)
text = request.values.get("text", "Default")
text_array = text.split("*")
user_response = text_array[-1]
level = len(text_array)
if text == "":
response = "CON WELCOME To 99's Mobile Banking Service\nAre you a:\n"
response += "1.Registered Member\n"
response += "2.New member\n"
elif text == "1":
response = "CON WELCOME BACK\nENTER Account Number:\n"
response=user.select()
elif text == "1"+user_response and level==2:
response = "CON To:\n"
response += "1.Check Account Balance\n"
response += "2.Deposit money\n"
response += "3.Withdraw Cash\n"
response += "4.Print Account MiniStatement\n"
elif text=="1*1"+user_response and level==3:
response = "END Your Balance is KSH:\n"
response = bank.view_balance()
elif text=="1*2"+user_response and level==3:
response = "CON Enter Amount\n"
response=bank.deposit()
elif text_array[0]+user_response and level==4:
response=bank.deposit()
response="END Deposit successfull\n New Account Balance is KSH:\n"
response=bank.view_balance()
elif text=="1*3"+user_response and level==3:
response="CON Enter Amount\n"
elif text_array[0]+user_response and level==4:
response=bank.withdraw()
response="END Withdraw Successfull\n New Account balance is KSH:\n"
response=bank.view_balance()
elif text=="1*4"+user_response and level==3:
response="END Account Mini Statement:\n"
response=User.show_details()
response=bank.view_balance()
elif text=="2" and level==1:
response = "CON Welcome To 99's Mobile Banking Services \nEnter your Name:\n"
elif text=="2"+user_response and level==2:
response = "CON Enter your Age:\n"
elif text_array[0] and level==3:
response = "CON Enter your Gender:\n"
elif text_array[0] and level==4:
response = "CON Enter your Phonenumber:\n"
elif text_array[0] and level==5:
response = "CON Enter your Email:\n"
elif text_array[0] and level==6:
AccountNo=text_array[0]
Amount=text_array[1]
Amount2=text_array[2]
Nameofclient = text_array[3]
Age = text_array[4]
Gender = text_array[5]
Phonenumber = text_array[6]
Email = text_array[7]
user.insert(Nameofclient, Age, Gender, Phonenumber, Email,Amount,Amount2,AccountNo)
else:
response = "END Invalid Input"
return response
if __name__ == '__main__':
app.run()