我的测验中 else 的语法错误
Syntax error on else in my quiz
我是 Pythonista 程序的新手。我正在做一个测验,当我尝试测试它时出现语法错误。
这是错误:
我不熟悉 Pythonista,但是,通过快速搜索,它使用了显着的白色-space。在这种情况下,当您的 else 应该与其对应的 if 处于同一级别时,您的 else 就会缩进。以后还有类似的错误。
详情请看这里:http://omz-software.com/pythonista/docs/tutorial/controlflow.html
您的 else indentation
在第 10 行不正确
您的代码缩进正确:
def quiz():
score = 0
begin = raw_input("do you want to start ?")
if begin == "yes":
print "A : 56"
print "B : 48"
print "C : 45"
q1 = raw_input("what is 12*4")
if q1 in ["b","B"]:
print "congrats !! well done!1"
score += 1
else:
print "sorry!! you are wrong try next one !! good luck"
print "A : Another ice age"
print "B : A meteor will hit the earth"
print "C : Aliens will invade earth"
q2 = raw_input("what will happen in 50 years?")
if q2 in ["a","A"]:
print "nice !! keep going!1"
score += 1
else:
print "sorry!! you are wrong try next one !! good luck"
return score
else:
print "ok bye"
return 0
这只是对您的代码进行的修改。但我不会建议这种方法,因为你正在为这两个问题再次编写相同的代码 again.Instead 我会建议使用一个适当的数据结构并循环遍历它然后它将足够动态地做更多事情 quiz.You 可以像这样使用一组字典-
[{"question":"what is 5*4 ?","options":[10,20,30],"answer_index":1},{"question":"what is 10*4 ?","options":[40,50,60,30],"answer_index":0}]
我是 Pythonista 程序的新手。我正在做一个测验,当我尝试测试它时出现语法错误。
这是错误:
我不熟悉 Pythonista,但是,通过快速搜索,它使用了显着的白色-space。在这种情况下,当您的 else 应该与其对应的 if 处于同一级别时,您的 else 就会缩进。以后还有类似的错误。
详情请看这里:http://omz-software.com/pythonista/docs/tutorial/controlflow.html
您的 else indentation
在第 10 行不正确
您的代码缩进正确:
def quiz():
score = 0
begin = raw_input("do you want to start ?")
if begin == "yes":
print "A : 56"
print "B : 48"
print "C : 45"
q1 = raw_input("what is 12*4")
if q1 in ["b","B"]:
print "congrats !! well done!1"
score += 1
else:
print "sorry!! you are wrong try next one !! good luck"
print "A : Another ice age"
print "B : A meteor will hit the earth"
print "C : Aliens will invade earth"
q2 = raw_input("what will happen in 50 years?")
if q2 in ["a","A"]:
print "nice !! keep going!1"
score += 1
else:
print "sorry!! you are wrong try next one !! good luck"
return score
else:
print "ok bye"
return 0
这只是对您的代码进行的修改。但我不会建议这种方法,因为你正在为这两个问题再次编写相同的代码 again.Instead 我会建议使用一个适当的数据结构并循环遍历它然后它将足够动态地做更多事情 quiz.You 可以像这样使用一组字典-
[{"question":"what is 5*4 ?","options":[10,20,30],"answer_index":1},{"question":"what is 10*4 ?","options":[40,50,60,30],"answer_index":0}]