Python测验评分和条件语句问题
我在学校已经开始学习Python大约一个月了,我决定做一个小测验。我添加了一个评分系统,所以如果你回答错误,它会告诉你你的分数。但是,这不起作用,它总是会给你0分。还有,如果他们在一个问题上不及格,有没有一种方法可以只放一个陈述,而不是每个问题放一个陈述?谢谢:) 下面是一个代码示例(Python 3.2.3):Python测验评分和条件语句问题,python,Python,我在学校已经开始学习Python大约一个月了,我决定做一个小测验。我添加了一个评分系统,所以如果你回答错误,它会告诉你你的分数。但是,这不起作用,它总是会给你0分。还有,如果他们在一个问题上不及格,有没有一种方法可以只放一个陈述,而不是每个问题放一个陈述?谢谢:) 下面是一个代码示例(Python 3.2.3): 这是因为您在函数中看到的score变量是您设置的score变量的副本,通过值传递(我强烈建议您修改此主题)。你需要一种传递状态的方式 很快你就会发现对象,现在(将来再也不会有了!),一
这是因为您在函数中看到的
score
变量是您设置的score
变量的副本,通过值传递(我强烈建议您修改此主题)。你需要一种传递状态的方式
很快你就会发现对象,现在(将来再也不会有了!),一个简单的解决方案是将score
变量设为全局变量。替换
def question2(score):
与
在所有问题函数中,添加全局分数
,作为每个函数的第一个语句,如下所示:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
将所有出现的score2
替换为score
,您就完成了
当然,您可以对所有问题使用单个if:else
分支。我不会给出完整的解决方案,以便您可以练习,但这里有一个提示:创建一个包含三个参数的函数:
quick
。现在您可以这样使用它:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
这是因为您在函数中看到的
score
变量是您设置的score
变量的副本,通过值传递(我强烈建议您修改此主题)。你需要一种传递状态的方式
很快你就会发现对象,现在(将来再也不会有了!),一个简单的解决方案是将score
变量设为全局变量。替换
def question2(score):
与
在所有问题函数中,添加全局分数
,作为每个函数的第一个语句,如下所示:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
将所有出现的score2
替换为score
,您就完成了
当然,您可以对所有问题使用单个if:else
分支。我不会给出完整的解决方案,以便您可以练习,但这里有一个提示:创建一个包含三个参数的函数:
quick
。现在您可以这样使用它:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
这是因为您在函数中看到的
score
变量是您设置的score
变量的副本,通过值传递(我强烈建议您修改此主题)。你需要一种传递状态的方式
很快你就会发现对象,现在(将来再也不会有了!),一个简单的解决方案是将score
变量设为全局变量。替换
def question2(score):
与
在所有问题函数中,添加全局分数
,作为每个函数的第一个语句,如下所示:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
将所有出现的score2
替换为score
,您就完成了
当然,您可以对所有问题使用单个if:else
分支。我不会给出完整的解决方案,以便您可以练习,但这里有一个提示:创建一个包含三个参数的函数:
quick
。现在您可以这样使用它:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
这是因为您在函数中看到的
score
变量是您设置的score
变量的副本,通过值传递(我强烈建议您修改此主题)。你需要一种传递状态的方式
很快你就会发现对象,现在(将来再也不会有了!),一个简单的解决方案是将score
变量设为全局变量。替换
def question2(score):
与
在所有问题函数中,添加全局分数
,作为每个函数的第一个语句,如下所示:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
将所有出现的score2
替换为score
,您就完成了
当然,您可以对所有问题使用单个if:else
分支。我不会给出完整的解决方案,以便您可以练习,但这里有一个提示:创建一个包含三个参数的函数:
quick
。现在您可以这样使用它:
def question5():
global score
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score = score + 1
else:
print("you failed the quiz")
print("Score:", score)
quit()
quiz("When was the Great Fire of London?", ["1666", "1555", "1605"], "1666")
quiz("What is the capital of Australia?", ["Canberra", "Sydney", "Melbourne"], "Canberra")
声明如下
score2 = score2+1
在您的代码中没有任何效果,因为它们修改了局部变量
对于另一个问题:您应该使用一个数据结构来存储问题和答案,这样您就可以对它们进行迭代,而无需多次重复相同的代码
score2 = score2+1
在您的代码中没有任何效果,因为它们修改了局部变量
对于另一个问题:您应该使用一个数据结构来存储问题和答案,这样您就可以对它们进行迭代,而无需多次重复相同的代码
score2 = score2+1
在您的代码中没有任何效果,因为它们修改了局部变量
对于另一个问题:您应该使用一个数据结构来存储问题和答案,这样您就可以对它们进行迭代,而无需多次重复相同的代码
score2 = score2+1
在您的代码中没有任何效果,因为它们修改了局部变量
对于另一个问题:您应该使用数据结构来存储问题和答案,这样您就可以在不重复相同代码的情况下对它们进行迭代。如果这解决了您的问题,请:)如果这解决了您的问题,请:)如果这解决了您的问题,请:)如果这解决了您的问题,请