题目不重复10次,分数在最后
Questions not repeating 10 times with the score at the end
我正在 python 创建一个数学测验。我编写了代码,以便随机询问 10 个问题。在测验结束时,它应该告诉用户测验已经完成以及他们在测验中得分多少,但我在这方面遇到了一些困难。这是我的代码的一部分,我认为我出错了:
def askquestion():
score = 0
opslist = {operator.add: "+", operator.sub: "-", operator.mul: "x"} #All operators that can be chosen
num1,num2 = random.randint(1,10), random.randint(1,10) #Two Random Numbers
ops = random.choice(list(opslist.keys())) # random operators from oplist keys
ActualAnswer = (ops(num1,num2)) #Answer for my quiz
score = 0
print(num1,opslist[ops],num2) # Question for my quiz
userAns = (int(input("Enter answer:")))
if userAns == ActualAnswer: #If the user's answer matches the Actual Answer
print("Correct")
score = score + 1
else:
print("Incorrect")
score = score - 0
for i in range (10):
askquestion()
print ("The quiz has finished")
print ("Today you achieved a score of" ,score,"out of 10")
假设我将 for 循环移动到最后打印的下方,这样它就不是 def askquestion():
的一部分,我得到这样的输出:
2 + 6
Enter answer:8
Correct
The quiz has finished
Today you achieved a score of 1 out of 10
6 x 3
Enter answer:18
Correct
The quiz has finished
Today you achieved a score of 1 out of 10
5 x 1
Enter answer:5
Correct
The quiz has finished
Today you achieved a score of 1 out of 10
如果我将它保持在与其余代码一起的位置,它就不会问我问题,并且程序会在程序要求输入名称的介绍后停止。如果您认为您需要其余代码,请提供电子邮件地址,但我敢肯定,我在提供的代码中出错了。
Q.)我应该改什么才可以在10题结束时输出最后
你需要把你的 if/else 块放在你的 askquestion() 函数中或者在它之后的 for 循环中
从广义上讲,你想要这样的东西
def ask_question():
#your question logic here
#increment score
def print_summary(score):
print "Today you got a score of..."
for i in range(10):
ask_question()
print_summary()
定义在您的程序中执行每项任务的函数,然后指示您的程序应按这些函数运行的顺序。
您真正想要做的是将测验逻辑封装在 class 中,其中包含提问、管理测验和打印摘要方法。
您的代码中存在一些问题。这是我想出的解决方法。
import operator
import random
def askquestion():
score = 0
opslist = {operator.add: "+", operator.sub: "-", operator.mul: "x"} #All operators that can be chosen
num1,num2 = random.randint(1,10), random.randint(1,10) #Two Random Numbers
ops = random.choice(list(opslist.keys())) # random operators from oplist keys
ActualAnswer = (ops(num1,num2)) #Answer for my quiz
score = 0
print(num1,opslist[ops],num2) # Question for my quiz
userAns = (int(input("Enter answer:")))
if userAns == ActualAnswer: #If the user's answer matches the Actual Answer
print("Correct")
return 1
else:
print("Incorrect")
score = score - 0
return 0
totalScore = 0
for i in range (10):
totalScore += askquestion()
print ("The quiz has finished")
print ("Today you achieved a score of" ,totalScore,"out of 10")
您的所有 askQuestion()
函数都很好,但函数中的变量不是全局的,因此您需要 return 它以便稍后使用。不必跟踪在函数中创建的变量,您可以使用全局变量 totalScore
来跟踪分数。 askQuestion()
现在 return 如果答案正确则为 1,如果答案错误则为 0。
现在您需要将 for 循环移出 askQuestion()
才能正常工作。 for i in range(10):
有效,但您也可以像这样使用 while
循环:
totalScore = 0
i = 0
while i < 10:
totalScore += askQuestion()
i += 1
在循环中,您正在使用全局变量 totalScore
来跟踪 askQuestion()
的 1 和 0 return。最后,您只是打印乐谱。
我正在 python 创建一个数学测验。我编写了代码,以便随机询问 10 个问题。在测验结束时,它应该告诉用户测验已经完成以及他们在测验中得分多少,但我在这方面遇到了一些困难。这是我的代码的一部分,我认为我出错了:
def askquestion():
score = 0
opslist = {operator.add: "+", operator.sub: "-", operator.mul: "x"} #All operators that can be chosen
num1,num2 = random.randint(1,10), random.randint(1,10) #Two Random Numbers
ops = random.choice(list(opslist.keys())) # random operators from oplist keys
ActualAnswer = (ops(num1,num2)) #Answer for my quiz
score = 0
print(num1,opslist[ops],num2) # Question for my quiz
userAns = (int(input("Enter answer:")))
if userAns == ActualAnswer: #If the user's answer matches the Actual Answer
print("Correct")
score = score + 1
else:
print("Incorrect")
score = score - 0
for i in range (10):
askquestion()
print ("The quiz has finished")
print ("Today you achieved a score of" ,score,"out of 10")
假设我将 for 循环移动到最后打印的下方,这样它就不是 def askquestion():
的一部分,我得到这样的输出:
2 + 6
Enter answer:8
Correct
The quiz has finished
Today you achieved a score of 1 out of 10
6 x 3
Enter answer:18
Correct
The quiz has finished
Today you achieved a score of 1 out of 10
5 x 1
Enter answer:5
Correct
The quiz has finished
Today you achieved a score of 1 out of 10
如果我将它保持在与其余代码一起的位置,它就不会问我问题,并且程序会在程序要求输入名称的介绍后停止。如果您认为您需要其余代码,请提供电子邮件地址,但我敢肯定,我在提供的代码中出错了。
Q.)我应该改什么才可以在10题结束时输出最后
你需要把你的 if/else 块放在你的 askquestion() 函数中或者在它之后的 for 循环中
从广义上讲,你想要这样的东西
def ask_question():
#your question logic here
#increment score
def print_summary(score):
print "Today you got a score of..."
for i in range(10):
ask_question()
print_summary()
定义在您的程序中执行每项任务的函数,然后指示您的程序应按这些函数运行的顺序。
您真正想要做的是将测验逻辑封装在 class 中,其中包含提问、管理测验和打印摘要方法。
您的代码中存在一些问题。这是我想出的解决方法。
import operator
import random
def askquestion():
score = 0
opslist = {operator.add: "+", operator.sub: "-", operator.mul: "x"} #All operators that can be chosen
num1,num2 = random.randint(1,10), random.randint(1,10) #Two Random Numbers
ops = random.choice(list(opslist.keys())) # random operators from oplist keys
ActualAnswer = (ops(num1,num2)) #Answer for my quiz
score = 0
print(num1,opslist[ops],num2) # Question for my quiz
userAns = (int(input("Enter answer:")))
if userAns == ActualAnswer: #If the user's answer matches the Actual Answer
print("Correct")
return 1
else:
print("Incorrect")
score = score - 0
return 0
totalScore = 0
for i in range (10):
totalScore += askquestion()
print ("The quiz has finished")
print ("Today you achieved a score of" ,totalScore,"out of 10")
您的所有 askQuestion()
函数都很好,但函数中的变量不是全局的,因此您需要 return 它以便稍后使用。不必跟踪在函数中创建的变量,您可以使用全局变量 totalScore
来跟踪分数。 askQuestion()
现在 return 如果答案正确则为 1,如果答案错误则为 0。
现在您需要将 for 循环移出 askQuestion()
才能正常工作。 for i in range(10):
有效,但您也可以像这样使用 while
循环:
totalScore = 0
i = 0
while i < 10:
totalScore += askQuestion()
i += 1
在循环中,您正在使用全局变量 totalScore
来跟踪 askQuestion()
的 1 和 0 return。最后,您只是打印乐谱。