我正在运行以下脚本进行验证:
def num_val(x):
while True:
try:
userInput = raw_input("Please, enter the length of list " + x + " (only values from 1 to 100 are valid): ")
val = int(userInput)
except ValueError:
print("Entered value must be a number. Please, write the number again")
continue
if userInput < 1 or userInput > 100:
print ("Entered value must be a number in range from 1 to 100. Please write the number again")
else:
break
num_val("a")
我收到以下消息:
print ("Entered value must be a number in range from 1 to 100. Please write the number again")
^
IndentationError: expected an indented block
你能告诉我这条线的问题是什么吗?
答案 0 :(得分:2)
通过复制和粘贴,您的代码可能会混合使用空格和制表符,这可能会导致奇怪的错误。以下内容适用于我:
def num_val(x):
while True:
try:
userInput = raw_input("Please, enter the length of list " + x + " (only values from 1 to 100 are valid): ")
val = int(userInput)
except ValueError:
print("Entered value must be a number. Please, write the number again")
continue
if val < 1 or val > 100: # <-- use "val" instead of "userInput"
print ("Entered value must be a number in range from 1 to 100. Please write the number again")
else:
break
num_val("a")
如何预防这些问题?取决于你的编辑。许多编辑都有一个设置来扩展选项卡&#34;或者其他一些,对于Python代码,你应该利用这个功能。这样,当你点击 Tab 时,编辑器将插入空格。在Python中,Tab characters line up every 8 columns和其他缩进处的空格可能会使解析器混淆。
另请注意,if
语句应使用val
而不是userInput
,因为val
是数字,userInput
是文字。