Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在用python编写文本文件后,如何继续我的程序?_Python - Fatal编程技术网

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

在用python编写文本文件后,如何继续我的程序?,python,Python,这是我非常低效的代码。 很抱歉 无论如何,在函数addAccount()中,它会向文本文件添加一个新的帐户,但如果您尝试循环程序,它不会,我尝试使用一个名为menus()的函数进行谨慎循环,但除非程序结束,否则它拒绝打印到ext文件。以下是文本文件: Gri33415,Griffiths,2015,黄金,3540000 Smi22316,史密斯,2016,银,37500 Mia56213,Miah,2013,白金,140165000 All78915,Allen,2015,白金,12014500

这是我非常低效的代码。 很抱歉 无论如何,在函数addAccount()中,它会向文本文件添加一个新的帐户,但如果您尝试循环程序,它不会,我尝试使用一个名为menus()的函数进行谨慎循环,但除非程序结束,否则它拒绝打印到ext文件。以下是文本文件:

Gri33415,Griffiths,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 利华利44213,利华,2013,银,1537500

任何帮助都会很好,我在编码方面很差。

试试这个:

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()

注意:-)
而True
表示无止境。您必须使用
+c

中断此操作。您需要将调用菜单放在函数末尾,而不是在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()   

    while True:
        menu()
无论如何,您不应该这样做,因为这将导致
运行时错误:在很长一段时间内超过了最大递归深度。最好使用循环:

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()
   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