1

I have C++ background and very new to Python. I might be making a simple mistake.

def make_polish(s) :
    no_of_pluses = 0
    polish_str = []
    i = 0
    for index in range(len(s)):
        print s[index]
        if '+' == s[index]:
            no_of_pluses = no_of_pluses + 1
        if '*' == s[index]:
            polish_str[i] = s[index-1] """Index out of range error here."""
            i = i + 1 
            polish_str[i] = s[index+1]
            i = i + 1
            polish_str[i] = '*'
            i = i + 1

    return polish_str 

print make_polish("3*4")
4
  • Can you please reformat the code so it's copy-pasteable into a Python REPL. Commented Jun 8, 2015 at 4:23
  • Please edit your code so that it has more consistent indentation. Also, mixing tabs and spaces is a very bad idea in python Commented Jun 8, 2015 at 4:25
  • Please suggest a light weight Python editor for windows. I used notepad++ for it. Commented Jun 8, 2015 at 5:21
  • @qqqqq Try PyScripter Commented Jun 22, 2015 at 11:49

1 Answer 1

8

Your list polish_str is always empty. You need to do:

polish_str.append(s[index-1])

Instead of:

polish_str[i] = s[index-1] # """Index out of range error here."""
i = i + 1 

When you create the list polish_str = [] it's not allocating space for it as it does in C/C++. It's a dynamic data structure.

Sign up to request clarification or add additional context in comments.

1 Comment

Thansk it worked. It seems it is a linked list and I have to do a hidden "new" of C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.