import random
def findAccount():
component1 = open('SampleData2017.txt','r')
component2 = component1.readlines()
accountOne = component2[0].split(',')
accountTwo = component2[1].split(',')
accountThree = component2[2].split(',')
accountFour = component2[3].split(',')
accountFive = component2[4].split(',')
accountSix = component2[5].split(',')
accountSeven = component2[6].split(',')
accountEight = component2[7].split(',')
menuOption = input("What is the account holders surname?> ")
if menuOption == "Griffiths":
print ("Account ID: ",accountOne[0],
"\nAccount holder: ",accountOne[1],
"\nYear opened: ",accountOne[2],
"\nMembership status: ",accountOne[3],
"\nNo. of nights stayed: ",accountOne[4],
"\nAmount of points: ",accountOne[5])
menus()
elif menuOption == "Smith":
print ("Account ID: ",accountTwo[0],
"\nAccount holder: ",accountTwo[1],
"\nYear opened: ",accountTwo[2],
"\nMembership status: ",accountTwo[3],
"\nNo. of nights stayed: ",accountTwo[4],
"\nAmount of points: ",accountTwo[5])
menus()
elif menuOption == "Miah":
print ("Account ID: ",accountThree[0],
"\nAccount holder: ",accountThree[1],
"\nYear opened: ",accountThree[2],
"\nMembership status: ",accountThree[3],
"\nNo. of nights stayed: ",accountThree[4],
"\nAmount of points: ",accountThree[5])
menus()
elif menuOption == "Allen":
print ("Account ID: ",accountFour[0],
"\nAccount holder: ",accountFour[1],
"\nYear opened: ",accountFour[2],
"\nMembership status: ",accountFour[3],
"\nNo. of nights stayed: ",accountFour[4],
"\nAmount of points: ",accountFour[5])
menus()
elif menuOption == "Hugget":
print ("Account ID: ",accountFive[0],
"\nAccount holder: ",accountFive[1],
"\nYear opened: ",accountFive[2],
"\nMembership status: ",accountFive[3],
"\nNo. of nights stayed: ",accountFive[4],
"\nAmount of points: ",accountFive[5])
menus()
elif menuOption == "Selby":
print ("Account ID: ",accountSix[0],
"\nAccount holder: ",accountSix[1],
"\nYear opened: ",accountSix[2],
"\nMembership status: ",accountSix[3],
"\nNo. of nights stayed: ",accountSix[4],
"\nAmount of points: ",accountSix[5])
menus()
elif menuOption == "Santus":
print ("Account ID: ",accountSeven[0],
"\nAccount holder: ",accountSeven[1],
"\nYear opened: ",accountSeven[2],
"\nMembership status: ",accountSeven[3],
"\nNo. of nights stayed: ",accountSeven[4],
"\nAmount of points: ",accountSeven[5])
menus()
elif menuOption == "Leewah":
print ("Account ID: ",accountEight[0],
"\nAccount holder: ",accountEight[1],
"\nYear opened: ",accountEight[2],
"\nMembership status: ",accountEight[3],
"\nNo. of nights stayed: ",accountEight[4],
"\nAmount of points: ",accountEight[5])
menus()
else:
findAccount()
component1.close()
def menus():
endOption = input("Do you want to continue?, press Y or N> ")
if endOption == "Y":
menu()
elif endOption == "N":
exit()
else:
menus()
def addAccount():
component2 = open('SampleData2017.txt','a')
component4 = input("What is your name?> ")
component5 = str(input("What are the last two digits of the current
year?> "))
component9 = str(input("What is the year?> "))
component6 = str(random.randint(1,999))
component3 = component2.write(component4)
component3 = component2.write(component6)
component3 = component2.write(component5)
component8 = component2.write(',')
component8 = component2.write(component4)
component8 = component2.write(',')
component10 = component2.write(component9)
component11 = component2.write(',')
component12 = component2.write("Silver")
component13 = component2.write(',')
component14 = component2.write("0")
component15 = component2.write(',')
component16 = component2.write("0\n")
print("\n\n")
menus()
def menu():
choice = input("What is thy bidding?\nEnter either Find Account or Add
Account> \n\n")
if choice == "Find Account":
findAccount()
elif choice == "Add Account":
addAccount()
else:
menu()
menu()
这是我非常低效的代码。 抱歉。 无论如何,在函数addAccount()中,它向文本文件添加了一个新帐户,但是如果你尝试循环程序,它没有,我尝试使用一个名为menus()的函数来制作一个谨慎的循环但是它拒绝除非程序结束,否则打印到ext文件。这是文本文件:
Gri33415,格里菲思,2015,金,35,40000 Smi22316,史密斯,2016年,银,3,7500 Mia56213,Miah,2013年,铂金,140,165000 All78915,艾伦,2015年,铂金,120,145000 Hug91714,Huggett,2014年,铂金,150,50000 Sel77617,塞尔比,2017年,黄金,40,45000 San55614,Santus,2014年,银,12,30000 Lee44213,Leewah,2013年,银,15,37500
任何帮助都会很棒,我在编码时非常糟糕。
答案 0 :(得分:1)
您需要将调用菜单放在函数的末尾,而不是在else部分:
def menu():
choice = input("What is thy bidding?\nEnter either Find Account or Add
Account> \n\n")
if choice == "Find Account":
findAccount()
elif choice == "Add Account":
addAccount()
menu()
无论如何,你不应该这样做,因为它会在很长一段时间内导致RuntimeError: maximum recursion depth exceeded
。更好地使用循环:
def menu():
continue = True
while (continue):
choice = input("What is thy bidding?\nEnter either Find Account or Add
Account> \n\n")
if choice == "Find Account":
findAccount()
elif choice == "Add Account":
addAccount()
# Update continue if you need to stop
答案 1 :(得分:0)
试试这个:
def menu():
choice = input("What is thy bidding?\nEnter either Find Account or Add Account> \n\n")
if choice == "Find Account":
findAccount()
elif choice == "Add Account":
addAccount()
while True:
menu()
注意:-) while True
意味着无穷无尽。您必须使用<ctrl>+c