这段代码中有些问题需要解决。
首先,在open_account函数内部,您需要声明user_info是一个全局变量。所以在函数内部的开始处添加一行`global user_info`即可。
其次,在check_balance、withdraw_money和deposit_money函数内部,您应该使用`user_info[card_number]`来访问和修改字典中的值。
最后,在transfer_money函数内部,您应该使用`user_info[from_card_number]`和`user_info[to_card_number]`来访问和修改字典中的值。
以下是修正后的代码:import random# 用户信息存储字典user_info = {}# 随机生成6位数字卡号def generate_card_number(): return ''.join(random.choices('1234567890', k=6))# 开户功能def open_account(): global user_info name = input("请输入姓名:") id_number = input("请输入身份证号:") phone_number = input("请输入手机号:") initial_deposit = float(input("请输入预存金额:")) password = input("请输入密码:") card_number = generate_card_number() while card_number in user_info: card_number = generate_card_number() user_info[card_number] = { "name": name, "id_number": id_number, "phone_number": phone_number, "balance": initial_deposit, "password": password } print(f"您的卡号为:{card_number}")# 查询功能def check_balance(card_number, password): if card_number not in user_info or user_info[card_number]["password"] != password: return False balance = user_info[card_number]["balance"] print(f"您的卡中余额为:{balance}") return True# 取款功能def withdraw_money(card_number, password): if not check_balance(card_number, password): return balance = user_info[card_number]["balance"] amount = float(input("请输入取款金额:")) if amount > balance or amount < 0: print("输入的取款金额有误,请重新输入。") return user_info[card_number]["balance"] -= amount print(f"取款后卡中余额为:{user_info[card_number]['balance']}")# 存款功能def deposit_money(card_number, password): if not check_balance(card_number, password): return balance = user_info[card_number]["balance"] amount = float(input("请输入存款金额:")) if amount < 0: print("输入的存款金额有误,请重新输入。") return user_info[card_number]["balance"] += amount print(f"存款后卡中余额为:{user_info[card_number]['balance']}")# 转账功能def transfer_money(): from_card_number = input("请输入转出卡号:") from_password = input("请输入转出卡密码:") if not check_balance(from_card_number, from_password): return to_card_number = input("请输入转入卡号:") if to_card_number not in user_info: print("转入卡号不存在。") return amount = float(input("请输入转账金额:")) confirm = input("确认执行转账操作吗?(y/n):") if confirm.lower() == 'y': if amount > user_info[from_card_number]["balance"]: print("转账金额超过转出卡余额。") return user_info[from_card_number]["balance"] -= amount user_info[to_card_number]["balance"] += amount print("转账成功。") else: print("转账取消。")# 锁定和解锁功能def lock_unlock_card(card_number, password, operation): if card_number not in user_info or user_info[card_number]["password"] != password: return False if operation == 'lock': user_info[card_number]["locked"] = True print("卡已锁定。") elif operation == 'unlock': user_info[card_number]["locked"] = False print("卡已解锁。") else: print("操作错误。")# 主程序def main(): global user_info while True: print("\n***********************************************") print("*** 功能菜单 ***") print("*** 1.开户(1) 2.查询(2) ***") print("*** 3.取款(3) 4.存款(4) ***") print("*** 5.转账(5) 6.锁定(6) ***") print("*** 7.解锁(7) ***") print("*** 退出(Q) ***") print("***********************************************") choice = input("请选择功能:") if choice == '1': open_account() elif choice == '2': card_number = input("请输入卡号:") password = input("请输入密码:") if not check_balance(card_number, password): print("密码错误或卡号不存在,请重新输入。") elif choice == '3': card_number = input("请输入卡号:") password = input("请输入密码:") withdraw_money(card_number, password) elif choice == '4': card_number = input("请输入卡号:") password = input("请输入密码:") deposit_money(card_number, password) elif choice == '5': transfer_money() elif choice == '6': card_number = input("请输入卡号:") password = input("请输入密码:") lock_unlock_card(card_number, password, 'lock') elif choice == '7': card_number = input("请输入卡号:") password = input("请输入密码:") lock_unlock_card(card_number, password, 'unlock') elif choice.lower() == 'q': break else: print("输入有误,请重新选择。")if __name__ == "__main__": main()
在此修正后的代码中,我还添加了一个"locked"字段来记录用户是否锁定了他们的账户。
希望这可以帮助到您!如果您还有其他问题,请随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |