【发布时间】:2015-05-20 19:31:27
【问题描述】:
我是一个完整的新手程序员,并且正在制作一个基本的“会员”计划,以给我一些函数和列表方面的经验 到目前为止我有this:
#Club membership program with functions
Club = ['George','Isaiah','Alby','Jack']
#FUNCTIONS=========================================================================================================================================================================================================
def remove_member():
"""Function to delete member from the club"""
print('The club members are:', Club)
removal_member = str(input('Enter the name of the member you would like to remove:'))
if removal_member in Club:
Club.remove(removal_member)
else:
print('Member not found')
print('The club members are now:', Club)
def add_member():
"""Funtion to add a member to the club"""
new_member = str(input('Enter the name of the member you would like to add:'))
Club.append(new_member)
print('The club members are now:', Club)
def functions():
print('''
List of current funtions:
1 = Remove a member
2 = Add a member
3 = trherjuthyjht\n''')
#MAIN==================================================================================================================================================================================================================
print('HELLO AND WELCOME TO THE CLUB MEMBERS PROGRAM')
print('The current members of the club are:', Club)
functions()
run_program = input('Would you like to carry out a function? (y/n):')
while run_program == 'y':
function_no = int(input('enter the the number of the funtion you would like to execute:'))
if function_no == 1:
remove_member()
run_program = input('Would you like to carry out a function? (y/n):')
functions()
else:
if function_no == 2:
add_member()
run_program = input('Would you like to carry out a function? (y/n):')
functions()
input('Thanks you for using the Club membership program, press enter to exit.')
接下来我想做的是扩展我的文件处理技能: 我希望能够将列表写入文件,以便每次打开或关闭程序时都可以检索列表
- 我该怎么做
- 我需要将它保存为什么文件类型,以便 python 将其识别为列表
任何进一步的帮助将不胜感激
【问题讨论】:
-
下一次,请在您的帖子中包含您的代码,而不是在外部链接中。这次我把它编辑到你的帖子里了
-
有许多不同的格式可以写入文件。您是否要将数据写入文件,例如文件的每一行都有一个名称,然后解析输入?还是您真的要保存数据结构列表及其所有成员?
-
@GC123UNC 该文件纯粹是供程序使用的,因此人类不需要使用,因此无论程序何时检索下次打开程序时的列表,最好的方法都是