Python 中的 ATM。如何将第一个代码中的数据库连接到第二个代码中的事务?阅读正文
ATM in Python. How can I connect the database in the first code to the transaction in the second code? read body
我写了2段代码
- 登录和身份验证
def register():
db = open("database.txt","r")
account_number= input("Enter your Account Number: ")
pin = input("Create a 4-digit pin: ")
pin1 = input("Confirm Pin: ")
d = []
f = []
for i in db:
a,b = i.split(", ")
b = b.strip()
d.append(a)
f.append(b)
data = dict(zip(d,f))
print(data)
if pin != pin1:
print("Pins don't match, try again!")
register()
else:
if len(pin) != 4:
print("Pin is not 4-digit. Pin must be 4-digit")
register()
elif account_number in d:
print("account number already exists")
register()
else:
db = open("database.txt","a")
db.write(account_number+", "+pin+", 0" "\n")
print("Success!")
db.close()
def access():
db = open("database.txt", "r")
account_number = input("Enter your account number: ")
pin = input("Enter your pin: ")
if not len(account_number or pin)<1:
d = []
f = []
for i in db:
a, b = i.split(", ")
b = b.strip()
d.append(a)
f.append(b)
data = dict(zip(d, f))
try:
if data[account_number]:
try:
if pin== data[account_number]:
print("Login Success")
else:
print("Pin or Account Number Incorrect")
except:
print("Pin or Account Number Incorrect")
else:
print("Account Number doesn't exist")
except:
print("Login error")
else:
print("Please Enter your login credentials")
db.close()
def home(option = None):
option = input("Login | Signup: ")
if option == "Login":
access()
elif option == "Signup":
register()
else:
print("Please choose an option")
home()
- 货币交易
choice = 0
while choice != 4:
print("\n\n**** Menu *****")
print("1 -- balance")
print("2 == deposit")
print("3 == withdraw")
print("4 == cancel\n")
choice=int(input("\nEnter your option:\n"))
if choice==1:
print("Balance = ", +balance)
elif choice==2:
dep=int(input("Enter your deposit: "))
balance+=dep
print("\n deposited amount: " , +dep)
print("balance = ", +balance)
elif choice==3:
wit=int(input("Enter the amount to withdraw: "))
balance -= wit
print("\n withdrawn amount: " , +wit)
print("balance =", +balance)
elif choice ==4:
print('Session ended,goodbye.')
else:
print("Invalid Entry")
第一个代码在数据库中存储了帐号和密码。新帐户的余额应自动为 0。如何自动将余额设置为0并存入数据库?
只允许数值怎么办?
如何将第二个代码连接到第一个代码中的数据库?
您的第一个问题是如何自动创建一个余额为 0 的帐户。除非我弄错了并且 0 是您已经在使用此行创建的其他内容:db.write(account_number+", "+pin+", 0" "\n")
.
我不知道你遇到了什么问题,但我可以看到一个潜在的问题,因为你在 write('w') 模式下打开数据库,然后在 append('a' 模式下打开它) 模式。不能同时做,要先关掉。
你的第二个问题回答起来有点复杂,你要么必须创建 类 要么以不同的方式构建你的程序。
为了方便我们自己,我们只需将数据库加载到内存中。这已经是大部分逻辑了,在主循环中实现你的菜单:
DB_FILE = "database.txt"
db = {}
with open(DB_FILE, 'r') as f:
db_lines = f.read().splitlines()
for line in db_lines:
acc_num, pin, balance = line.split()
db[acc_num] = {'pin': pin, 'balance': int(balance)}
def update_db():
with open(DB_FILE, 'w') as f:
for acc_num, data in db.items():
f.write(acc_num + " " + data['pin'] + " " + str(data['balance']))
def register(acc_num, pin):
db[acc_num] = {'pin' : pin, 'balance': 0}
update_db()
def access(acc_num, pin):
if db[acc_num]['pin'] == pin:
return True
return False
def withdraw(acc_num, amount):
db[acc_num]['balance'] -= amount
update_db()
def deposit(acc_num, amount):
db[acc_num]['balance'] += amount
update_db()
login = False
while True:
if not login:
choice = input('register or access')
if choice == 'access':
acc_num = input('accnum')
pin = input('pin')
if access(acc_num, pin):
login = True
elif choice == 'register':
# get acc num + pin
register(acc_num, pin)
login = True
else:
# withdraw and deposit
我写了2段代码
- 登录和身份验证
def register():
db = open("database.txt","r")
account_number= input("Enter your Account Number: ")
pin = input("Create a 4-digit pin: ")
pin1 = input("Confirm Pin: ")
d = []
f = []
for i in db:
a,b = i.split(", ")
b = b.strip()
d.append(a)
f.append(b)
data = dict(zip(d,f))
print(data)
if pin != pin1:
print("Pins don't match, try again!")
register()
else:
if len(pin) != 4:
print("Pin is not 4-digit. Pin must be 4-digit")
register()
elif account_number in d:
print("account number already exists")
register()
else:
db = open("database.txt","a")
db.write(account_number+", "+pin+", 0" "\n")
print("Success!")
db.close()
def access():
db = open("database.txt", "r")
account_number = input("Enter your account number: ")
pin = input("Enter your pin: ")
if not len(account_number or pin)<1:
d = []
f = []
for i in db:
a, b = i.split(", ")
b = b.strip()
d.append(a)
f.append(b)
data = dict(zip(d, f))
try:
if data[account_number]:
try:
if pin== data[account_number]:
print("Login Success")
else:
print("Pin or Account Number Incorrect")
except:
print("Pin or Account Number Incorrect")
else:
print("Account Number doesn't exist")
except:
print("Login error")
else:
print("Please Enter your login credentials")
db.close()
def home(option = None):
option = input("Login | Signup: ")
if option == "Login":
access()
elif option == "Signup":
register()
else:
print("Please choose an option")
home()
- 货币交易
choice = 0
while choice != 4:
print("\n\n**** Menu *****")
print("1 -- balance")
print("2 == deposit")
print("3 == withdraw")
print("4 == cancel\n")
choice=int(input("\nEnter your option:\n"))
if choice==1:
print("Balance = ", +balance)
elif choice==2:
dep=int(input("Enter your deposit: "))
balance+=dep
print("\n deposited amount: " , +dep)
print("balance = ", +balance)
elif choice==3:
wit=int(input("Enter the amount to withdraw: "))
balance -= wit
print("\n withdrawn amount: " , +wit)
print("balance =", +balance)
elif choice ==4:
print('Session ended,goodbye.')
else:
print("Invalid Entry")
第一个代码在数据库中存储了帐号和密码。新帐户的余额应自动为 0。如何自动将余额设置为0并存入数据库?
只允许数值怎么办?
如何将第二个代码连接到第一个代码中的数据库?
您的第一个问题是如何自动创建一个余额为 0 的帐户。除非我弄错了并且 0 是您已经在使用此行创建的其他内容:db.write(account_number+", "+pin+", 0" "\n")
.
我不知道你遇到了什么问题,但我可以看到一个潜在的问题,因为你在 write('w') 模式下打开数据库,然后在 append('a' 模式下打开它) 模式。不能同时做,要先关掉。
你的第二个问题回答起来有点复杂,你要么必须创建 类 要么以不同的方式构建你的程序。
为了方便我们自己,我们只需将数据库加载到内存中。这已经是大部分逻辑了,在主循环中实现你的菜单:
DB_FILE = "database.txt"
db = {}
with open(DB_FILE, 'r') as f:
db_lines = f.read().splitlines()
for line in db_lines:
acc_num, pin, balance = line.split()
db[acc_num] = {'pin': pin, 'balance': int(balance)}
def update_db():
with open(DB_FILE, 'w') as f:
for acc_num, data in db.items():
f.write(acc_num + " " + data['pin'] + " " + str(data['balance']))
def register(acc_num, pin):
db[acc_num] = {'pin' : pin, 'balance': 0}
update_db()
def access(acc_num, pin):
if db[acc_num]['pin'] == pin:
return True
return False
def withdraw(acc_num, amount):
db[acc_num]['balance'] -= amount
update_db()
def deposit(acc_num, amount):
db[acc_num]['balance'] += amount
update_db()
login = False
while True:
if not login:
choice = input('register or access')
if choice == 'access':
acc_num = input('accnum')
pin = input('pin')
if access(acc_num, pin):
login = True
elif choice == 'register':
# get acc num + pin
register(acc_num, pin)
login = True
else:
# withdraw and deposit