2017-09-04 71 views
0
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文件,除非程序结束。这里是文本文件:如何在写入python文本文件后继续执行程序?

Gri33415,格里菲思,2015年,黄金,35,40000 Smi22316,史密斯,2016年,银,3,7500 Mia56213,Miah,2013年,铂金,140,165000 All78915,艾伦, 2015,Platinum,120,145000 Hug91714,Huggett,2014,Platinum,150,50000 Sel77617,Selby,2017,Gold,40,45000 San55614,Santus,2014,Silver,12,30000 Lee44213,Leewah,2013,银,15,37500

任何帮助将是伟大的,我很不好在编码。

回答

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

1

你需要把调用菜单功能的结束,而不是在其他部分中断此:

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 
相关问题