2014-11-05 2 views
-1

파일 assign2_partI_test_file을 실행하고 아래 결과를 얻습니다. 나는 다만 정확한 결과를 얻을 수 없다.함수가 테스트 결과와 일치하지 않습니다.

내 코드 :

def to_string(my_list, sep=', '): 

    result = ' ' 
    msg = 'List is: ' 
    for char in my_list: 

     str_list1 = ['r', 'i', 'n', 'g', 'i', 'n', 'g'] 

     if my_list == str_list1: 

      result = msg + sep.join(my_list) 

     return result 

내 출력 :

Start testing! 

length Test 

Start Testing! 

length Test 
List length: 7 
List length: 0 

to_string Test 
List is: r, i, n, g, i, n, g 
List is: r-i-n-g-i-n-g 
None # (THIS IS SUPPOSED TO DISPLAY: List is:) 

테스트 코드 :

import list_function 

print("\nStart Testing!") 

str_list1 = ['r', 'i', 'n', 'g', 'i', 'n', 'g'] 
str_list2 = ['r', 'e', 'd'] 
empty = [] 

print("\nlength Test") 
print("List length:", list_function.length(str_list1)) 
print("List length:", list_function.length(empty)) 

print("\nto_string Test") 
string = list_function.to_string(str_list1) 
print(string) 
string = list_function.to_string(str_list1, sep='-') 
print(string) 
print(list_function.to_string(empty)) 

print("\nEnd Testing!\n") 
+0

테스트 코드가 도움이되었습니다. 문제를 명확하게 보여줍니다. 입력이 빈 목록 인 경우 코드가 올바른 작업을 수행하지 않습니다. 그래서 그것을 고치려고 무엇을 했습니까? 현재 코드는'for' 루프 안에있는 것만을'return'합니다. 아마도 당신은 그것에 대해 더 신중하게 생각해야합니다. – jonrsharpe

+0

좋아, 나는 오늘의 1/2 이상을 시도했다. 이것은 가장 가까운 것이고, 나는 의도 한 결과를 얻을 수 있습니다. – Macrick

+0

@Macrick 내 대답을 참조하십시오 –

답변

0

I 리뷰 등이 더 대답 할거야 :

def to_string(my_list, sep=', '): 

    result = ' ' 
    # only needed because of the odd return 
    msg = 'List is: ' 
    # fine, but you only use it once, so why not put the literal there? 
    for char in my_list: 
    # why loop? 

     str_list1 = ['r', 'i', 'n', 'g', 'i', 'n', 'g'] 
     # hard-coded the test... 

     if my_list == str_list1: 
     # ...so you guarantee it only works for one input - why?! 

      result = msg + sep.join(my_list) 
      # this line is useful 

     return result 
     # but only inside the for loop?! 

기능을 한 줄로 단축 할 수 있습니다 (거의 작성 했으니 까!).

def to_string(my_list, sep=', '): 
    if my_list == ['r', 'i', 'n', 'g', 'i', 'n', 'g']: # test case 
     return 'List is: ' + sep.join(my_list) 
    elif len(my_list) > 0: # any other non-empty list 
     return ' ' 
    else: # empty list 
     return None 

합니까 상황이 명확하게 :

여기

는 함수의 출력과 일치하는 단순화 된 기능입니다? 이 세 가지 경우 사이에 큰 차이가 있습니까?

+0

진지하게, 의도가 없다면 대답하지 마십시오. 심각하게 감사합니다. – Macrick

+0

@Macrick 당신은 무엇을 기대 했습니까? 이것은 코드 작성 서비스가 아니며 귀하의 실수를 강조하려고 노력했습니다. 당신은 실제로 올바른 코드를 작성한 후 논리적으로 이해할 수없는 많은 것들로 그것을 이해할 수 없게 둘러 쌌습니다. – jonrsharpe

+0

감사합니다. 안녕하세요 – Macrick

0

elifelse 부분에 다른 조건을 쓰지 않는 이유는 무엇입니까? 한 조건, 즉 my_list == str_list1에 대해서만 작성했습니다. 그러나 다른 조건은 어떻게됩니까? 예를 들어 empty 목록에 있습니까? 그것도 확인하고 싶을 것입니다.

def to_string(my_list, sep=', '): 
    result = ' ' 
    msg = 'List is: ' 
    str_list1 = ['r', 'i', 'n', 'g', 'i', 'n', 'g'] 
    if my_list == str_list1: 
     result = msg + sep.join(my_list) 
    elif my_list == []:   ## you can also use "len(my_list) == 0:" 
     result = msg 
    return result 

이제 문제 None # (THIS IS SUPPOSED TO DISPLAY: List is:)을 가지고있어 이유는 for 루프이었다. for char in my_list:my_list이 비어 있지 않은 경우에만 실행됩니다. 그러나 빈 목록을 전달할 때 for 루프는 반복 할 것이 없기 때문에 루프가 실행되지 않습니다.

+0

감사합니다. 건배 – Macrick

+0

@Macrick Welcome. 내 대답이 당신을 도왔다 고 생각한다면 떠날 때 upvote 및 수락을 잊지 마세요! :) –

관련 문제