0% found this document useful (0 votes)
11 views

CSP Create Task

The document outlines a Python program for a math game called 'Addition Master' where users can play at different difficulty levels (Easy, Medium, Hard, and Bonus) by solving addition problems. Players can track their statistics, including wins and losses, and can exit the game at any time. The program includes user input validation for usernames and provides a main menu for game options such as rules, play, stats, and exit.

Uploaded by

mc.server.q2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

CSP Create Task

The document outlines a Python program for a math game called 'Addition Master' where users can play at different difficulty levels (Easy, Medium, Hard, and Bonus) by solving addition problems. Players can track their statistics, including wins and losses, and can exit the game at any time. The program includes user input validation for usernames and provides a main menu for game options such as rules, play, stats, and exit.

Uploaded by

mc.server.q2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1 import random

2 import time
3 end_list = ["No"]
4 stats_list = [0,0,1,0,0] # [Wins, Losses, Level, Difficulty, Way to end the game after user
wins]
5
6
7 while True: #Asks user for their username
8 username = input("Enter your username: (No Spaces) ")
9 if len(username) < 2:
10 print("\nThe username is too short.\n")
11 elif len(username) > 14:
12 print("\nThe username is too long.\n")
13 else:
14 break
15
16 def main():
17
18 print("\nWelcome to the Addition Master.\n")
19 while True: #Makes the code repeat until user stops it manually or until user beats the
game
20
21 if end_list[0] == "Yes": #A way to end the game if the user wants to exit
22 break
23
24 #Sets the difficulty for the game
25 if stats_list[3] == 0:
26 difficulty = "Easy"
27 if stats_list[3] == 1:
28 difficulty = "Medium"
29 if stats_list[3] == 2:
30 difficulty = "Hard"
31 if stats_list[3] == 3:
32 difficulty = "Bonus"
33
34 choice = input("Type: R for Rules, P for Play, S for Stats, E for Exit: ") #Asks user of
what they want to do
35
36 #Call to the main_menu function
37 main_menu(choice, difficulty)
38
39 #If the user has beaten all the levels
40 if stats_list[4] == 1:
41 print("Congratulations " + username + "! You won the game!\n")
42 print("You have won a prize of being 0.01% better at adding numbers than before!")
43 time.sleep(3)
44 print("\nFinal Stats:")
45 print("You have guessed correctly " + str(stats_list[0]) + " times.")
46 print("You have guessed incorrectly " + str(stats_list[1]) + " times.\n")
47 break
48
49
50
51
52 #The game function
53 def game(difficulty, stats_list):
54
55
56 #If difficulty is easy
57 if difficulty == "Easy":
58 correct_answer = 0
59 num_list = []
60 for i in range(int(stats_list[2]) + 1): #Adds one more than the level that user is on
61 num_list.append(random.randint(0,10)) #Adds random numbers to the list based on
difficulty
62 nums_sum = int(input("\nWhat is the sum of these numbers: " + str(num_list) + " "))
63 for i in range(len(num_list)):
64 correct_answer += int(num_list[i]) #Find the correct answer by traversing a list
65 if nums_sum == correct_answer:
66 print("\nCorrect!\n")
67 stats_list[0] += 1 #Adds 1 to the correct guesses
68 stats_list[2] += 1 #Adds 1 to the level
69 if stats_list[2] == 6: #If user has beaten all 5 levels, resets back to level 1 and
advances a difficulty
70 stats_list[3] += 1
71 stats_list[2] = 1
72 print("Great job " + username + "! You have unlocked the Medium difficulty.\n")
73 else:
74 print("\nIncorrect.\n")
75 stats_list[1] += 1 #Adds 1 to the incorrect guesses
76
77
78
79 #If difficulty is medium
80 elif difficulty == "Medium":
81 correct_answer = 0
82 num_list = []
83 for i in range(int(stats_list[2]) + 1): #Adds one more than the level that user is on
84 num_list.append(random.randint(0,20)) #Adds random numbers to the list based on
difficulty
85 nums_sum = int(input("\nWhat is the sum of these numbers: " + str(num_list) + " "))
86 for i in range(len(num_list)):
87 correct_answer += int(num_list[i]) #Find the correct answer by traversing a list
88 if nums_sum == correct_answer:
89 print("\nCorrect!\n")
90 stats_list[0] += 1 #Adds 1 to the correct guesses
91 stats_list[2] += 1 #Adds 1 to the level
92 if stats_list[2] == 6: #If user has beaten all 5 levels, resets back to level 1 and
advances a difficulty
93 stats_list[3] += 1
94 stats_list[2] = 1
95 print("Great job " + username + "! You have unlocked the Hard difficulty.\n")
96 else:
97 print("\nIncorrect.\n")
98 stats_list[1] += 1 #Adds 1 to the incorrect guesses
99
100
101
102 #If difficulty is hard
103 elif difficulty == "Hard":
104 correct_answer = 0
105 num_list = []
106 for i in range(int(stats_list[2]) + 1): #Adds one more than the level that user is on
107 num_list.append(random.randint(-10,50)) #Adds random numbers to the list
based on difficulty
108 nums_sum = int(input("\nWhat is the sum of these numbers: " + str(num_list) + "
"))
109 for i in range(len(num_list)):
110 correct_answer += int(num_list[i]) #Find the correct answer by traversing a list
111 if nums_sum == correct_answer:
112 print("\nCorrect!\n")
113 stats_list[0] += 1 #Adds 1 to the correct guesses
114 stats_list[2] += 1 #Adds 1 to the level
115 if stats_list[2] == 6: #If user has beaten all 5 levels, resets back to level 1 and
advances a difficulty
116 stats_list[3] += 1
117 stats_list[2] = 1
118 print("Great job " + username + "! You have beaten all the difficulties and
unlocked the bonus level.\n")
119 else:
120 print("\nIncorrect.\n")
121 stats_list[1] += 1 #Adds 1 to the incorrect guesses
122
123
124
125 #Bonus level
126 elif difficulty == "Bonus":
127 correct_answer = 0
128 num_list = []
129 for i in range(int(stats_list[2]) + 2): #Adds one more than the level that user is on
130 num_list.append(random.randint(-100,1500)) #Adds random numbers to the list
based on difficulty
131 nums_sum = int(input("\nWhat is the sum of these numbers: " + str(num_list) + "
"))
132 for i in range(len(num_list)):
133 correct_answer += int(num_list[i]) #Finds the correct answer by traversing a list
134 if nums_sum == correct_answer:
135 print("\nCorrect!\n")
136 stats_list[0] += 1 #Adds 1 to the correct guesses
137 stats_list[4] += 1 #Changes the value so the loop will break since the user has
beaten the game
138 else:
139 print("\nIncorrect.\n")
140 stats_list[1] += 1 #Adds 1 to the incorrect guesses
141
142
143 return difficulty, stats_list #Returns the difficulty and the stats_list
144
145
146
147
148
149 #Main menu function
150 def main_menu(choice, difficulty):
151
152 #Prints the rules of the game
153 if choice.upper() == "R":
154 print("\nLoading...\n")
155 time.sleep(2)
156 print("Welcome to the Addition Master - A math game where your goal is to add
all the numbers given. There are 3 difficulties - Easy, Medium and Hard. You will start on
the Easy difficulty. To access Medium difficulty you have to beat all 5 levels of the Easy
difficulty. To access Hard difficulty you have to beat 5 levels of the Medium difficulty. If
you manage to beat all the levels you will unlock a bonus level and if you beat it, you will
get a prize. Good luck " + username + "!\n")
157
158 #Starts the next level of the game
159 elif choice.upper() == "P":
160 game(difficulty, stats_list)
161
162 #Shows user their statistics
163 elif choice.upper() == "S":
164 print("\nLoading...\n")
165 time.sleep(2)
166 print("You are on the level " + str(stats_list[2]) + " of the " + str(difficulty) + "
difficulty.")
167 print("You have guessed correctly " + str(stats_list[0]) + " times.")
168 print("You have guessed incorrectly " + str(stats_list[1]) + " times.\n")
169
170 #Exits the game
171 elif choice.upper() == "E":
172 end_game = input("\nAre you sure you want to end the game? (Your progress
will be lost) Yes or No: ")
173 if end_game.lower() == "yes" or end_game.lower() == "y":
174 print("\nThank you for playing " + username + "!")
175 print("\nYou were on the level " + str(stats_list[2]) + " of the " + difficulty + "
difficulty.")
176 print("You have guessed correctly " + str(stats_list[0]) + " times.")
177 print("You have guessed incorrectly " + str(stats_list[1]) + " times.")
178 end_list[0] = "Yes"
179 else:
180 print("\nReturning back to the main menu...\n")
181 time.sleep(2)
182
183 #If the input is invalid
184 else:
185 print("\nInvalid choice.\n")
186
187 #Call to the main function
188 main()
189
190 #Credits:
191 #Lines 43, 155, 165 and 181 ( time.sleep() ) were made freely available by:
https://www.digitalocean.com/community/tutorials/python-time-sleep

You might also like