而python中的while循环给出了语法错误

时间:2012-12-21 04:49:18

标签: python wlst

我是python的新手,这是我尝试的第一个程序。 此函数从标准输入中读取密码。

def getPassword() :
     passwordArray =[]
         while 1:
                 char = sys.stdin.read(1)
                 if char == '\\n':
                         break
                 passwordArray.append(char)
                 return passwordArray

print (username)
print (URL)

收到此错误:

Problem invoking WLST - Traceback (innermost last):
  (no code object) at line 0
  File "/scratch/aime/work/stmp/wlstCommand.py", line 10
                 while 1: 
                 ^
SyntaxError: invalid syntax

3 个答案:

答案 0 :(得分:3)

您的缩进不正确。您的while应缩进与其上方的行相同。

答案 1 :(得分:0)

Python使用缩进来“分离”东西,而那就是你需要在整个文件中使用相同类型的缩进。在你编写的代码中有一个固定的缩进是很好的做法。您可能需要考虑一个或四个空格(后面是PEP8样式指南中的建议)

答案 2 :(得分:0)

Python很敏感,并且依赖于缩进。如果它抱怨“格式无效”,则优于“无效语法”。