Python Quiz Scoring and Conditional Statement Problems -


i've started learning python @ school around month now, , have decided make quiz. have added in scoring system if answer question incorrectly, tell score. however, not working , give score of 0. also, there way of putting 1 else statement if fail question, instead of 1 each question? :)

here's example of code(python 3.2.3):

#quiz   print("welcome quiz") print("please choose difficulty:") difficulty = input("a) hard   b)easy")   if difficulty == "a":     score = 0     print("") def question(score):      print("you chose hard difficulty")      print("where great victoria lake located?")      answer1 = input("a) canada   b)west africa   c)australia   d)north america")      if answer1 == "c":          print("correct")          score = score+1      else:          print("you failed quiz")          print("score:",score)          quit()      def question2(score):      print("who responsible cracking enigma code")      answer2 = input("a) alan turing   b) jeff bezos   c) george boole   d) charles   babbage")      if answer2 == "a":          print("correct")          score = score+1      else:           print("you failed quiz")          print("score:",score)          quit()     def diff_easy(difficulty):     if difficulty == "b":         score2 = 0         print("")   def question4(score2):         print("you chose easy difficulty")         print("what capital of australia?")         answer1 = input("a) canberra   b) sydney   c)melbourne")         if answer1 == "a":             print("correct")             score2 = score2+1  else:          print("you failed quiz")          print("score:",score2)          quit()   def question5(score2):     print("when great fire of london?")     answer2 = input("a) 1666   b) 1555   c)1605")     if answer2 == "a":          print("correct")          score2 = score2+1      else:          print("you failed quiz")          print("score:",score2)          quit()  if difficulty == "a":     question(score)     question2(score)    if difficulty == "b":     diff_easy(difficulty)     question4(score2)     question5(score2) 

this because score variable see inside functions copy of score variable set, being passed by value (i strongly suggest revise topic). need way pass status around.

soon discover objects, (and never more in future!), simple solution making score variable global. replace

def question2(score): 

with

def question2(): 

in all of question functions , add global score first statement in each, this:

def question5():     global score     print("when great fire of london?")     answer2 = input("a) 1666   b) 1555   c)1605")     if answer2 == "a":          print("correct")          score = score + 1      else:          print("you failed quiz")          print("score:", score)          quit() 

replace occurences of score2 score , done.

of course can use single if: else branch questions. not give full solution, can exercise, here hint: make function take 3 arguments:

  1. the question
  2. a list of possible answers
  3. the correct answer

let's call function quiz. can use this:

quiz("when great fire of london?", ["1666", "1555", "1605"], "1666") quiz("what capital of australia?", ["canberra", "sydney", "melbourne"], "canberra") 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -