2013-12-22 5 views
0

사용자가 입력 한 단어와 숫자의 목록을 순서대로 반환하려하지만 모듈을 실행할 때 단어와 값을 입력하면 단어와 값 대신에 없음이 인쇄됩니다. 값을 순서대로.값과 단어 목록을 순서대로 반환하려고 시도합니다.

dictionary = [] 

value = [] 

addterm1 = raw_input("Enter a term you would like to add to the dictionary: ") 
addterm2 = raw_input("Enter a term you would like to add to the dictionary: ") 
addterm3 = raw_input("Enter a term you would like to add to the dictionary: ") 

addvalue1 = float(raw_input("Enter a number you would like to add to the set of values: ")) 
addvalue2 = float(raw_input("Enter a number you would like to add to the set of values: ")) 
addvalue3 = float(raw_input("Enter a number you would like to add to the set of values: ")) 

dictionary.append(addterm1) 
dictionary.append(addterm2) 
dictionary.append(addterm3) 

value.append(addvalue1) 
value.append(addvalue2) 
value.append(addvalue3) 

def reverseLookup(dictionary, value): 

    print dictionary.sort() 

    print value.sort() 


if __name__ == '__main__': 
    reverseLookup(dictionary, value) 
+0

당신은 예를 들어, 루프,이 훨씬 짧은 만들 수 할 수있는 코드를 해결하려면 'for range (3) : value.append (float (raw_input (...))'연속 된 줄에 같은 문자열을 반복하는 것은 쓸데없는 선물이다. – jonrsharpe

답변

0

list.sort는 현재 위치에서 방법이므로 항상 None을 반환 . 따라서 모든 호출은 자체 회선에 배치해야합니다.

여전히 list.sort 사용하려는 경우이 같은 코드를 만들 수 있습니다

def reverseLookup(dictionary, value): 
    dictionary.sort() 
    value.sort() 
    print dictionary 
    print value 

을 또는 당신이 sorted 사용할 수 있습니다 또한

def reverseLookup(dictionary, value): 
    print sorted(dictionary) 
    print sorted(value) 

을, 다른 이름을 선택 할 수 있습니다 목록이므로 dictionary의 경우 dictionary이 아닙니다.

1

.sort() 방법은, 그것은 현재 위치에서반복 가능한 정렬 정렬되지 return 않습니다. 당신은 다음print, sort 필요 : 또는

dictionary.sort() 
print(dictionary) 

, return을 수행하는 sorted() 기능을 사용하여 정렬 된 반복 가능한 :

print(sorted(dictionary)) 
0

두 가지 다른 기능이 있습니다. sorted()list.sort() (현재 사용중인 이름)

sorted() 정렬 목록을 반환합니다. 예 :

>>> a = [3, 1, 5, 2, 4] 
>>> print sorted(a) 
[1, 2, 3, 4, 5] 

아마도 원하는 작업입니다.

list.sort()기능 정확히 동일한 방식입니다. 그러나 을 반환하지 않습니다. 정렬 된 목록. 대신, 목록 에 정렬합니다.

>>> a = [3, 1, 5, 2, 4] 
>>> a.sort() 
>>> print a 
[1, 2, 3, 4, 5] 

대부분의 내부 기능은 파이썬 반환 None입니다. 그래서 당신이하려는 것은 :

>>> a = [3, 1, 5, 2, 4] 
>>> a = a.sort() 
>>> print a 
None 

, 당신은 print sorted(dictionary)print sorted(values)

관련 문제