can explain piece of code allow me understand def functions better, need know how code below acts counter , adds score @ end of test,:
for in range(questions): #ask 10 questions if quiz(): score +=1 this code test created, if can explained alot, can maybe give alternative version of piece of code, whole code below :
import random import math import operator op name = input("what name? ") print("hi {}! welcome arithmetic quiz...".format(name)) score = 0 questions = 10 def quiz(): x = random.randint(1, 10) y = random.randint(1, 10) ops = {'+': op.add,'-': op.sub,'*': op.mul} keys = list(ops.keys()) opt = random.choice(keys) operation = ops[opt] answer = operation(x, y) print ("what {} {} {}?".format(x, opt, y)) #prints question user_answer= int(input("your answer: ")) #prompts users answer if user_answer != answer: #validate users answer correct answer print ("incorrect. right answer is",answer"") return false else: print("correct!") return true in range(questions): #ask 10 questions if quiz(): score +=1 #counter print("{}: got {}/{} questions correct.".format(name, score, questions,))#'question' if (score==1) else 'questions' thanks <3
i need know how code @ top of page allows counter add correct answers in quiz
formatting matters in python, take care:
for in range(questions): #ask 10 questions if quiz(): score +=1 quiz() gets called 10 times, , returns true if question answered correctly. in case, variable score incremented.
Comments
Post a Comment