要附加到txt文件的参数太多

时间:2017-05-19 09:58:28

标签: append global

输入在第一个函数中收集,在第二个函数中可用,但我需要能够将所有内容附加到文本文件中。它说了太多的论据,请帮忙!

def newMember(): # gathers info to pass into sub routine to write to file
    global surname, year, status, nights, points
    print("Please enter the following information")
    surname=input("Surname\n")
    year=input("Year joined \n")
    status=input("Membership status \n")
    nights=input("Nights booked \n")
    points=input("Points balance \n")
    print("Please confirm these details \nSurname: "+surname+ "\nYear joined: "+year+ "\nMembership status: "+status+"\nNights booked: "+nights+ "\nPoints balance: "+points)
    reply=input("Please type either Yes or No\n")
    if reply in["Y","y","Yes","yes"]: #using an array in the selection to cover possible inputs
        print("Thank you, these will now be written to file.  Returning you to the main menu options") # write to file
        time.sleep(3)
        appendMember()
    elif reply in ["n","N","no","No"]: 
        print("Sorry, you will now be prompted to enter the details again")
        newMember()
    else:
        print("I'm sorry, I didn't quite catch that, the program will now restart")
        newMember()



def appendMember():
    global surname, year, status, nights, points
    with open('SampleData2017.txt', mode="a")as crawdale:
        crawdale.write[surname, year, status, nights, points]

1 个答案:

答案 0 :(得分:0)

您需要传递一个参数来写入文件。您可以将列表转换为逗号分隔的字符串,如此

def appendMember():
    global surname, year, status, nights, points
    with open('SampleData2017.txt', mode="a") as crawdale:
        crawdale.write(",".join([surname, year, status, nights, points]))