Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indentation error with python 3.3 when python2.7 works well

I wrote this script below which converts number to it's spelling.

no = raw_input("Enter a number: ")

strcheck = str(no)
try:
     val = int(no)
except ValueError:
     print("sayi degil")
     raise SystemExit
lencheck = str(no)
if len(lencheck) > 6:
     print("Bu sayi cok buyuk !")
     raise SystemExit

n = int(no)
print(n)
def int2word(n):

     n3 = []
     r1 = ""

     ns = str(n)
     for k in range(3, 33, 3):
              r = ns[-k:]
              q = len(ns) - k

    if q < -2:
        break
    else:
        if  q >= 0:
            n3.append(int(r[:3]))
        elif q >= -1:
            n3.append(int(r[:2]))
        elif q >= -2:
            n3.append(int(r[:1]))
    r1 = r

#print(n3)  


     nw = ""
     for i, x in enumerate(n3):
              b1 = x % 10
              b2 = (x % 100)//10
              b3 = (x % 1000)//100

          if x == 0:
                  continue  
              else:
                  t = binler[i]

              if b2 == 0:
                  nw = birler[b1] + t + nw
              elif b2 == 1:
                  nw = onlar[1] + birler[b1] + t + nw
              elif b2 > 1:
                  nw = onlar[b2] + birler[b1] + t + nw
              if b3 > 0:
                  nw = birler[b3] + "yuz " + nw
     return nw

 birler = ["", " ","iki ","uc ","dort ", "bes ", "alti ","yedi ","sekiz ","dokuz "]
 onlar = ["", "on ", "yirmi ", "otuz ", "kirk ", "elli ", "altmis ", "yetmis ", "seksen ", "doksan "]
 binler = ["", "bin"]

 print int2word(n)

This scripts works pretty well on Python2.7.

But when I try to run it with python3.3

It gives me error below:

File "numtospell.py", line 58
    if x == 0:
             ^
TabError: inconsistent use of tabs and spaces in indentation

I've googled it for hours but cannot find a suitable solution. What do I do to fix this?

Thanks for any help.

like image 733
cankemik Avatar asked Oct 10 '13 12:10

cankemik


People also ask

Why do I keep getting indentation error in Python?

The cause of Indentation Error in Python Since python makes use of procedural language, if you miss out on adding tabs or spaces between your lines of code, then you will most likely experience this error.

How do I get rid of unexpected indent error in Python?

“IndentationError: unexpected indent” is raised when you indent a line of code too many times. To solve this error, make sure all of your code uses consistent indentation and that there are no unnecessary indents.

Which version of code will produce an indentation error when it is run?

Python is a language where the code is arranged through whitespaces. If there is an incorrect indentation, this will result in an error, and the python interpreter will just return an error function. It uses the PEP8 whitespace ethics. There should be 4 whitespaces used between any alternative or iteration.


1 Answers

You are mixing tabs and spaces.

Python 3 explicitly disallows this. Use spaces only for indentation.

Quoting from the Python Style Guide (PEP 8):

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Python 3 disallows mixing the use of tabs and spaces for indentation.

Emphasis mine.

Almost all editors can be configured to replace tabs with spaces when typing, as well as do a search and replace operation that replaces existing tabs with spaces.

like image 191
Martijn Pieters Avatar answered Oct 23 '22 10:10

Martijn Pieters