Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1_8_maths_game_2.py
- # Have a go at adding more questions.
- # Also save a running log of the Score & Question numbers.
- # Would prefer to log the mark for each question
- # Also save the High score
- high_score = 0
- fw = open("1_8_high_score.txt", "w") # Overwrite. Must contain something eg '0'
- fa = open("1_8_score.txt", "a") # write Append
- fa.write("Score" + ", Question \n")
- while True: # means loop forever.
- score = 0
- print("Welcome to the Maths Quiz")
- print("Can you answer three questions and score maximum points?")
- # Q1
- print("Question 1: What is the product of 2x2x2 ?")
- answer = int(input("Your answer to Question 1 :>> "))
- if answer == 8:
- print("Correct")
- score = score + 1
- print("Your score is", score)
- fa.write(str(score) + ", \t#1\n")
- else:
- print("Incorrect, the answer is 8")
- print("Your score is", str(score))
- fa.write(str(score) + ", \t#1\n")
- # Q2
- print("Question 2: What is the product of 3x3 ?")
- answer = int(input("Your answer to Question 2 :>> "))
- if answer == 9:
- print("Correct")
- score = score + 1
- print("Your score is", score)
- fa.write(str(score) + ", \t#2\n")
- else:
- print("Incorrect, the answer is 9")
- print("Your score is", str(score))
- fa.write(str(score) + ", \t#2\n")
- # Q3
- print("Question 3: What is the product of 5x2 ?")
- answer = int(input("Your answer to Question 3 :>> "))
- if answer == 10:
- print("Correct")
- score = score + 1
- print("Your score is", score)
- fa.write(str(score) + ", \t#3\n")
- else:
- print("Incorrect, the answer is 10")
- print("Your score is", str(score))
- fa.write(str(score) + ", \t#3\n")
- if score > high_score:
- high_score = score
- print("The high score is", str(high_score))
- fw.write(str(high_score) + "\n")
- fa.close()
- fw.close()
- break
- # Save running score ...OK
- # Save high_score ... OK
- # Add more questions
- # "Question 1: What is the product of 2x2x2 ?", 8
- # "Question 2: What is the product of 3x3 ?", 9
- # "Question 3: What is the product of 5x2 ?", 10
Advertisement
Add Comment
Please, Sign In to add comment