我无法让这个python程序工作,每次运行它时,它会在第25行(即'else'循环)中出现“缩进时不一致使用制表符和空格”错误。我已经尝试了各种方法来解决这一切无济于事,所以想知道是否有人能解释我的问题。非常感谢您的时间。
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 :(得分:3)
您正在混合制表符和空格,使用-tt
运行python来检查:
python -tt scriptname.py
将编辑器配置为仅使用空格进行缩进。通常,有一个菜单选项可以将所有选项卡转换为空格。
显然,当您将代码粘贴到Stack Overflow窗口时,您已经可以看到您的else:
行仅使用空格,但其他行使用了标签:
if useranswer is correctanswer:
print("Correct, well done!")
score = score + 1
else:
print("Incorrect, sorry!")
因为Stack Overflow将标签渲染为4个空格。
答案 1 :(得分:1)
缩进代码时, 使用制表符或空格。不要混合它们(即你不能在一行上使用制表符和下一行使用制表符。)
答案 2 :(得分:1)
您的else
应与if
的缩进级别相同。
if useranswer is correctanswer:
print("Correct, well done!")
score = score + 1
else:
print("Incorrect, sorry!")
答案 3 :(得分:0)
至少在这个代码块上,你的if和else是不同的意图,它们应该是相同的。
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!")