我目前正在使用python中的小型银行应用程序。我创建了打开txt文件的函数。我自己测试了这个功能,所以我知道它有效,但是当它与菜单一起实现时似乎无法工作。
def parse_in():
with open('bank.txt', 'r') as details:
lines = details.read().splitlines()
return lines
后来我有一个简单的菜单,选项很少,但在这里我只会展示我遇到问题的菜单。
def menu():
print("Please selected one of the options" + "\n\t1: Open account"
while True:
try:
selection = int(input("Please enter your choice: "))
if selection == 1:
open_account(details)
else:
break
except:
print("Please enter number 1-6")
如果选择1,则应运行开立帐户
def open_account(details):
print("Welcome to your new account")
new_account_number = randint(000000, 999999)
while True:
try:
new_account_balance = float(input("Enter your sum of money: "))
if new_account_balance < 0:
print("please enter positive value")
else:
break
except:
print("Please Enter Number Only")
while True:
new_account_name = input("Enter your name: ")
if " " in new_account_name:
print("Please enter your full name")
else:
break
details.append(new_account_number)
details.append(new_account_balance)
details.append(new_account_name)
在推断信息之后,他们想要使用函数将其保存到同一文件中。
def parse_out(details):
with open('bank.txt', 'w') as file:
for i in details:
file.write(str(i) + '\n')
并且所有内容都在主要功能中运行
def main():
details = parse_in()
welcome()
menu()
parse_out(details)
当我按1时,打开的帐户功能似乎没有运行,菜单循环返回选择其中一个选项。我不确定为什么会这样做,因为它似乎应该运行并通过将信息保存到文件来获取用户信息。