2013-02-15 86 views
1

我不能完全得到这个python程序的工作,每次我运行它时,它都会出现在'else'循环的第25行'indentation中使用的标签和空格不一致'错误。我尝试了各种方法来解决这一切都无济于事,所以想知道是否有人可以解释我的问题。非常感谢您的时间。缩进问题

questions = ["What does RAD stand for?", 
     "Why is RAD faster than other development methods?", 
     "Name one of the 3 requirements for a user friendly system", 
     "What is an efficient design?", 
     "What is the definition of a validation method?"] 


answers = ["A - Random Applications Development, B - Recently Available Diskspace, C - Rapid Applications Development", 
     "A - Prototyping is faster than creating a finished product, B - Through the use of CASE tools, C - As end user evaluates before the dev team", 
     "A - Efficient design, B - Intonated design, C - Aesthetic design", 
     "A - One which makes best use of available facilities, B - One which allows the user to input data accurately, C - One which the end user is comfortable with", 
     "A - A rejection of data which occurs because input breaks predetermined criteria, B - A double entry of data to ensure it is accurate, C - An adaption to cope with a change external to the system"] 

correctanswers = ["C", "B", "A", "A", "A"] 

score = 0 

for i in range(len(questions)): 
    a = 1 
    print (questions[a]) 
    print (answers[a]) 
    useranswer = (input("Please enter the correct answer's letter here:")) 
    correctanswer = correctanswers[a] 
    if useranswer is correctanswer: 
      print("Correct, well done!") 
      score = score + 1 
     else: 
      print("Incorrect, sorry!") 

print("Well done, you scored" + score + "//" + int(len(questions))) 
+0

''else'loop' ?? - >否则,如果不是一个循环。 – 2013-02-15 12:06:05

+0

在'if'语句之后,直到'else'的'print',将每个语句向左移动4个空格。我认为这将解决问题。 – 2013-02-15 12:06:54

回答

1

当缩进代码,要么使用凸片空格。不要混合这些(即不能在一行上使用制表符和在下一行上使用制表符)。

+0

如果必须使用tab按钮 – Gjordis 2013-02-15 12:07:14

0

至少在这个codeblock上,你的if和else在不同的intendations上,它们应该是相同的。

if useranswer is correctanswer: 
     print("Correct, well done!") 
     score = score + 1 
    else: 
     print("Incorrect, sorry!") 

应该是:

if useranswer is correctanswer: 
     print("Correct, well done!") 
     score = score + 1 
else: 
     print("Incorrect, sorry!") 
1

else应该在if的相同的缩进水平。

if useranswer is correctanswer: 
     print("Correct, well done!") 
     score = score + 1 
    else: 
     print("Incorrect, sorry!") 
3

你混合制表符和空格,运行python与-tt检查:

python -tt scriptname.py 

配置编辑器来使用空格缩进。通常,还有一个菜单选项可将所有制表符转换为空格。

显然,当你粘贴代码到堆栈溢出窗口,你已经可以看到你的else:线仅使用空格,但其他线路使用标签:

if useranswer is correctanswer: 
      print("Correct, well done!") 
      score = score + 1 
     else: 
      print("Incorrect, sorry!") 

因为堆栈溢出呈现标签到4个空间。

+0

'if'和'else'必须具有相同的标识=相同数量的空格之前的空格,那么任何好的python编辑器都会在设置“将制表符转换为空格”中设置复选框 – User 2013-02-15 13:09:39