python2.7运行良好时python 3.3的缩进错误 [英] 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)
此脚本在 Python2.7 上运行良好.
This scripts works pretty well on Python2.7.
但是当我尝试使用 python3.3 运行它时
But when I try to run it with python3.3
它给了我以下错误:
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?
感谢您的帮助.
推荐答案
您正在混合使用制表符和空格.
You are mixing tabs and spaces.
Python 3 明确禁止这样做.使用空格仅作为缩进.
Python 3 explicitly disallows this. Use spaces only for indentation.
引自 Python 风格指南(PEP 8):
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 不允许混合使用制表符和空格进行缩进.
强调我的.
几乎所有编辑器都可以配置为在键入时用空格替换制表符,以及执行用空格替换现有制表符的搜索和替换操作.
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.
这篇关于python2.7运行良好时python 3.3的缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!