#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목문자열 뒤집기 프로그램 질문2019-03-13 01:23
작성자

Step_05_String의 문자열 뒤집기 프로그램에 질문이 있습니다.


저는 두 가지 방법으로 풀어 오류가 났습니다.


1.

def reverseString(ValR):

    print(ValR[4],ValR[3],ValR[2],ValR[1],ValR[0],sep="")

    return


2.

def reverseString(ValR):

    print(ValR[4],sep="",end="")

    print(ValR[3],sep="",end="")

    print(ValR[2],sep="",end="")

    print(ValR[1],sep="",end="")

    print(ValR[0],sep="")

    return


두가지 경우 모두

assert reverseString("hello") == "olleh"

assert reverseString("world") == "dlrow"

assert reverseString("12345") == "54321"

를 실행했을 때


olleh
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-77-80fe570984f9> in <module>
----> 1 assert reverseString("hello") == "olleh"
      2 assert reverseString("world") == "dlrow"
      3 assert reverseString("12345") == "54321"

AssertionError: 

과 같은 오류가 발생했습니다.

이러한 오류는 왜 발생하는 건지 궁금합니다.


(+)

그리고 추가적으로 인터넷 검색을 통해 찾아본 해결법으로는

3. print(ValR[ ::-1]
4. print(reversed(ValR)) 

이 있었습니다. 하지만 두가지 방법은

1.
olleh
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-12-80fe570984f9> in <module>
----> 1 assert reverseString("hello") == "olleh"
      2 assert reverseString("world") == "dlrow"
      3 assert reverseString("12345") == "54321"

AssertionError: 

2.
<reversed object at 0x000002667BAE4B38>
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-80fe570984f9> in <module>
----> 1 assert reverseString("hello") == "olleh"
      2 assert reverseString("world") == "dlrow"
      3 assert reverseString("12345") == "54321"

AssertionError: 

과 같은 오류가 발생했습니다.

어떻게 코드를 짜야 오류가 발생하지 않고 프로그램을 완성할 수 있나요?
댓글