귀하의 질문에 직접 대답하는 답변보다 나은 답변은 다음과 같습니다. dict
당신은 단순히 사용하는 것이
def getMovies():
movieinfo = {"Movie_Name": [], "Movie_Stars": [], "Movie_Budget": []}
print ("Your Movies")
amount_in_list = int(input("How many Movies? "))
x = 1
while x <= amount_in_list:
film = input ("Name of film ... ")
stars = input ("Main stars ...")
budget = input ("Budget ...")
movieinfo["Movie_Name"].append(film)
movieinfo["Movie_Stars"].append(stars)
movieinfo["Movie_Budget"].append(budget)
x+=1
return movieInfo
주의 사항 : 간단한 데모로
(프로그램이 사용자 정의 객체를 생성 원합니다 지점으로 개발하지 않는 한) 당신은 확실히이 상황에 대한 사전이 필요 key
문자열을 사용하여 해당 목록을 가져오고 (함수 시작 부분에서 초기화) 원하는대로 데이터를 추가합니다.
OP의 업데이트 된 요청에 대한 추가 정보를 제공하기 위해 편집 됨. 당신이 영화의 정보는 사용자가 주어진 단지 영화의 이름을 기준으로 찾으려면
, 당신은 이런 식으로 뭔가를 시도 할 수 :
film = 'The Matrix' # Assuming this is the user's input.
Try:
# The index method will throw an exception if
# the movie cannot be found. If that happens,
# the 'except' clause will execute and print
# the relevant statement.
mIdx = movieinfo['Movie_Name'].index(film)
print '{0} stars {1} and had a reported budget of {2}'.format(
film, movieInfo['Movie_Stars'][mIdx], movieInfo['Movie_Budget'][mIdx])
except ValueError:
print '{0} is not in the movie archives. Try another?'.format(film)
출력 :
'The Matrix stars Keanu Reeves and had a reported budget of $80 million'
또는 :
'The Matrix is not in the movie archives. Try another?'
'목록'이 아닌 '사전'을 사용해야합니다. – Matthias
Movie라는 이름으로 클래스를 만드는 것이 훨씬 쉬울 것입니다. 그러면 목록에 영화 인스턴스를 저장할 수 있습니다 ... –
어떻게해야합니까 @ kasperTaeymans –