TonyMo

1_8_maths_game.py

Apr 14th, 2021
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # 1_8_maths_game_2.py
  2. # Have a go at adding more questions.
  3. # Also save a running log of the Score & Question numbers.
  4. #   Would prefer to log the mark for each question
  5. # Also save the High score
  6.  
  7. high_score = 0
  8. fw = open("1_8_high_score.txt", "w")  # Overwrite. Must contain something eg '0'
  9. fa = open("1_8_score.txt", "a")  # write Append
  10. fa.write("Score" + ", Question \n")
  11.  
  12. while True:  # means loop forever.
  13.     score = 0    
  14.     print("Welcome to the Maths Quiz")
  15.     print("Can you answer three questions and score maximum points?")
  16. # Q1
  17.     print("Question 1: What is the product of 2x2x2 ?")
  18.     answer = int(input("Your answer to Question 1 :>> "))
  19.  
  20.     if answer == 8:
  21.         print("Correct")
  22.         score = score + 1
  23.         print("Your score is", score)
  24.         fa.write(str(score) + ", \t#1\n")
  25.     else:
  26.         print("Incorrect, the answer is 8")
  27.         print("Your score is", str(score))
  28.         fa.write(str(score) + ", \t#1\n")
  29. # Q2
  30.     print("Question 2: What is the product of 3x3 ?")
  31.     answer = int(input("Your answer to Question 2 :>> "))
  32.  
  33.     if answer == 9:
  34.         print("Correct")
  35.         score = score + 1
  36.         print("Your score is", score)
  37.         fa.write(str(score) + ", \t#2\n")
  38.     else:
  39.         print("Incorrect, the answer is 9")
  40.         print("Your score is", str(score))
  41.         fa.write(str(score) + ", \t#2\n")
  42. # Q3
  43.     print("Question 3: What is the product of 5x2 ?")
  44.     answer = int(input("Your answer to Question 3 :>> "))
  45.  
  46.     if answer == 10:
  47.         print("Correct")
  48.         score = score + 1
  49.         print("Your score is", score)
  50.         fa.write(str(score) + ", \t#3\n")
  51.     else:
  52.         print("Incorrect, the answer is 10")
  53.         print("Your score is", str(score))
  54.         fa.write(str(score) + ", \t#3\n")
  55.  
  56.     if score > high_score:
  57.         high_score = score
  58.     print("The high score is", str(high_score))
  59.  
  60.     fw.write(str(high_score) + "\n")
  61.    
  62.     fa.close()
  63.     fw.close()
  64.  
  65.     break
  66.  
  67. # Save running score ...OK
  68. # Save high_score ... OK
  69.  
  70. # Add more questions
  71. # "Question 1: What is the product of 2x2x2 ?", 8
  72. # "Question 2: What is the product of 3x3 ?",   9
  73. # "Question 3: What is the product of 5x2 ?",  10
Advertisement
Add Comment
Please, Sign In to add comment