파이썬 질문 드립니다.

익명 사용자의 이미지

#delall 이면 입력한 같은 원소 전부 삭제,
elif co=='delall':
i=input('Enter element: ')
while True:
if(i in tp):
li=list(tp)
for a in range(0,len(li)):
if (li[a]==i):
del li[a]
else:
break;

tp= tuple(li)
print(tp) #tuple 인쇄
else:
print('***', i, 'NOT in tuple ***')

이렇게 출력을 하면
if (li[a]==i):
IndexError: list index out of range
라고 오류가 뜨는데 범위를 어떻게 정해야할지 모르겠습니다.

dgkim의 이미지

1. 코드가 포함된 경우, (글 작성 화면의) 상단에 강조된(?) 문구를 참조하여, 주시면 감사합니다.
소스코드를 올릴 때에는 <code> 소스코드 내용 </code> 으로 올려 주세요.

2. 미리보기란 버튼을 활용하면 아주 좋습니다. 미리보기란, 본인이 작성한 글을 실제로 사용자에게 게시 및 오픈하기 전에, 어떤 형태로 보일 것인지를 미리 확인할 수 있도록 도와주는 기능입니다.

3. 아래는 소스코드 구문 강조를 활용한 예제입니다. ( 사용방법은 글 작성 화면에 나오는 추가 설명 내용을 참조하시고 2번항목에 언급한 '미리보기'를 활용하시기 바랍니다. )

print "Enter/Paste your content. Ctrl-D to save it."
contents = []
while True:
    try:
        line = raw_input("")
    except EOFError:
        break
    contents.append(line)
 
str_question = '\n'.join(contents)
 
if '<code>' in str_question or '<python>' in str_question:
    print("You did a great job")

들여쓰기 등 보기에 편한 형태로 작성됩니다.

4. 문제가 제대로 보이지 않으므로, 제대로 답변할 수 없습니다. ( 글을 올리신 분도, 웹을 통해서 글을 보실 수 있을 것이며, 코드가 정상적인 언어 구문이 아닌 것을 아실 것이라 생각합니다. )

5. 에러 메시지에 대한 힌트. 아마도 아래와 같은 사례가 아닌가 의심합니다.

>>> lst_123 = [ 1, 2, 3 ]
>>> lst_123[0]
1
>>> lst_123[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

(위 소스에서 발생하는 오류 상황은 추가 설명 없어도 이해하실 수 있으리라 생각합니다. 위 오류를 이해하지 못하는 경우, 쉽게 구할 수 있는 Python 책을 한번 먼저 읽어 보시기 바랍니다.)

6. 오류를 추적하는 방법 몇 가지.
소스 코드를 python 인터프리터에서 하나 하나 실행해 가면서 결과를 보면서 오류를 찾는다.
python pdb 모듈을 활용하여, 디버그 도구를 사용해 본다. 참조 : https://www.google.co.kr/search?q=python+pdb+example
pycharm 같은 통합 개발환경을 사용해 디버그 도구를 활용해 본다.

글쓴이의 이미지

네 감사합니다!!

글쓴이의 이미지

#delall 이면 입력한 같은 원소  전부 삭제, 
    elif co=='delall':
        i=input('Enter element: ')
        if(i in tp):
            li=list(tp)
            for a in range(0,len(li)):
                if (li[a]==i):
                    del  li[a]
                    break;
 
            tp= tuple(li)
            print(tp) #tuple 인쇄
        else:
            print('***', i, 'NOT in tuple ***')

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.