我的代码存在一个奇怪的问题。当我只想问10个问题时,它会问这个问题14次。然后它也没有正确计算得分。任何帮助,以解决这两个问题!提前致谢
import random
StudentName = input("Please enter you name: ")
def question_a():
score=0
num1=random.randint(1,10)
num2=random.randint(1,10)
num3=num1-num2
answer=int(input("What is "+str(num1)+"-"+str(num2)+"? "))
if answer==num3:
print ("Well done, you are correct")
score=score+1
else:
print ("That is the wrong answer")
return score
def question_b():
score=0
num1=random.randint(1,10)
num2=random.randint(1,10)
num3=num1+num2
answer=int(input("What is "+str(num1)+"+"+str(num2)+"? "))
if answer==num3:
score = score + 1
print ("Well done, you are correct")
else:
print ("That is the wrong answer")
return score
def question_c():
score=0
num1=random.randint(1,10)
num2=random.randint(1,10)
num3=num1*num2
answer=int(input("What is "+str(num1)+"*"+str(num2)+"? "))
if answer==num3:
score = score + 1
print ("Well done, you are correct")
else:
print ("That is the wrong answer")
return score
def question_d():
score=0
num1=random.randint(1,10)
num2=random.randint(1,10)
num3=num1/num2
num4=round(num3,1)
answer=float(input("What is "+str(num1)+"/"+str(num2)+"? "))
if answer==num4:
score = score + 1
print ("Well done, you are correct")
else:
print ("That is the wrong answer")
return score
my_list=[question_a,question_b,question_c,question_d]
for i in range (0,10):
random.choice(my_list)()
final_score = (question_a() + question_b() + question_c() + question_d())
print(StudentName+", you scored "+str(final_score)+"/10.")
答案 0 :(得分:2)
这一行:
random.choice(my_list)()
调用10个随机函数:对。但忽略输出:错误。
然后这一行:
final_score = (question_a() + question_b() + question_c() + question_d())
非随机调用这些函数并添加结果。 10 + 4等于14次调用您的函数。
解决方案:
final_score = 0
for i in range (0,10):
final_score += random.choice(my_list)()
或类似的东西。