在用python编写文本文件之后,如何继续我的程序?

2024-05-19 07:05:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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,黄金,3540000 Smi22316,史密斯,2016,银牌,37500 Mia56213,Miah,2013,白金,140165000 All78915,Allen,2015,白金,120145000 Hug91714,Huggett,2014,白金,15050000 Sel77617,Selby,2017,黄金,4045000 San55614,Santus,2014,银牌,1230000 Lee44213,利华,2013,银牌,1537500

任何帮助都会很好,我很不擅长编码


Tags: ofidaccountwritesplitprintmenuselif
2条回答

您需要将对菜单的调用放在函数的末尾,而不是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

试试这个:

    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来打断这个过程

相关问题 更多 >

    热门问题