python - Random function selecting -
a unusual issue code. asking question 14 times when want 10 questions asked. not calculating score correctly either. help appreciated prepare both of these issues! in advance
import random studentname = input("please come in name: ") def question_a(): score=0 num1=random.randint(1,10) num2=random.randint(1,10) num3=num1-num2 answer=int(input("what "+str(num1)+"-"+str(num2)+"? ")) if answer==num3: print ("well done, correct") score=score+1 else: print ("that wrong answer") homecoming score def question_b(): score=0 num1=random.randint(1,10) num2=random.randint(1,10) num3=num1+num2 answer=int(input("what "+str(num1)+"+"+str(num2)+"? ")) if answer==num3: score = score + 1 print ("well done, correct") else: print ("that wrong answer") homecoming score def question_c(): score=0 num1=random.randint(1,10) num2=random.randint(1,10) num3=num1*num2 answer=int(input("what "+str(num1)+"*"+str(num2)+"? ")) if answer==num3: score = score + 1 print ("well done, correct") else: print ("that wrong answer") homecoming 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 "+str(num1)+"/"+str(num2)+"? ")) if answer==num4: score = score + 1 print ("well done, correct") else: print ("that wrong answer") homecoming score my_list=[question_a,question_b,question_c,question_d] in range (0,10): random.choice(my_list)() final_score = (question_a() + question_b() + question_c() + question_d()) print(studentname+", scored "+str(final_score)+"/10.")
this line:
random.choice(my_list)()
calls 10 random function: right. ignores output: wrong.
then line:
final_score = (question_a() + question_b() + question_c() + question_d())
calls these functions non-randomly , adds results. 10 + 4 equals 14 calls functions.
solution:
final_score = 0 in range (0,10): final_score += random.choice(my_list)()
or that.
python function
No comments:
Post a Comment