필자가 작성한 프로그램 전체에서 목록을 사용하고 싶습니다. 기본적으로, 그것은 다른 사람들에 관한 정보를 가진 튜플들로 가득 찬리스트이고, 각 사람의 정보 (이름, 전화 번호, 주소 등)는 튜플 안에 저장됩니다. 나는 초기 함수를 통해이 목록을 정의하지만, 다른 사람들과 마찬가지로이 함수를 내 상호 작용 함수에 사용해야합니다.파이썬에서 다른 기능의 목록 사용
제 질문은이 목록을 전역 변수로 정의하지 않고 사용할 수 있습니까?
def load_friends(filename):
"""imports filename as a list of tuples using the import command"""
import csv
with open(filename, 'Ur')as filename:
friends_list = list(tuple(x) for x in csv.reader(filename, delimiter=','))
def add_friend(friend_info, friends_list):
"""appends the friend_info tupple to the list friends_list"""
new_list = friends_list.append(friends_info)
def interact():
"""interaction function: accepts user input commands"""
while True:
command = raw_input('Command: ')
기능을 수행하기 위해 입력을 구문 분석하는 명령이 있다는 것도 언급해야합니다. 이것은 목록 사용에 영향을 미칩니 까?
'load_friends'에서'friends_list'를 반환하고'add_friend'에서와 같이 다른 함수에 매개 변수로 전달할 수 있습니다. – Moshe