2017-09-04 48 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 
相關問題