11"""
2- This program is a Math Game with 3 levels and grading system accordingly.
3- Questions have 2 operands and 1 operator .
4- Teaches use of random module in Python.
5- Written in Python 2.7
6- written by: shreydan github.com/shreydan
7- """
2+ written in Python 3 by Shreyas Daniel - github.com/shreydan
3+ Game: 2 operators, 2 operands, 3 levels and grades accordingly .
4+
5+ ***********************************************
6+ * READ THIS CAREFULLY TO UNDERSTAND THE LOGIC *
7+ ***********************************************
88
9+ """
910
1011import random
1112
1213correct = 0
1314wrong = 0
1415
1516def result (correct , wrong , level ):
16- print "You got %s correct and %s wrong!" % (correct ,wrong )
17+ print ( "You got %s correct and %s wrong!" % (correct ,wrong ) )
1718 if level == 1 :
1819 if correct < 5 :
19- print "Grade: C"
20+ print ( "Grade: C" )
2021 elif correct >= 5 and correct <= 7 :
21- print "Grade: B"
22+ print ( "Grade: B" )
2223 else :
23- print "Grade: A"
24+ print ( "Grade: A" )
2425
2526 if level == 2 :
2627 if correct < 7 :
27- print "Grade: C"
28+ print "Grade: C" )
2829 elif correct >= 7 and correct <= 15 :
29- print "Grade: B"
30+ print ( "Grade: B" )
3031 else :
31- print "Grade: A"
32+ print ( "Grade: A" )
3233
3334 if level == 3 :
3435 if correct < 13 :
35- print "Grade: C"
36+ print ( "Grade: C" )
3637 elif correct >= 13 and correct <= 19 :
37- print "Grade: B"
38+ print ( "Grade: B" )
3839 else :
39- print "Grade: A"
40+ print ( "Grade: A" )
4041
4142def finalizer (level ,questions ,correct ,wrong ):
4243 if level == 1 and questions == 10 :
@@ -55,13 +56,13 @@ def calculator(numbers, operator, level, questions):
5556 elif operator == '*' :
5657 answer = float (numbers [0 ] * numbers [1 ])
5758
58- guess = float (raw_input ("\n \n " + str (numbers [0 ]) + " " + operator + " " + str (numbers [1 ]) + "\n " ))
59+ guess = float (input ("\n \n " + str (numbers [0 ]) + " " + operator + " " + str (numbers [1 ]) + "\n " ))
5960 global correct , wrong
6061 if guess == answer :
6162 correct += 1
6263 else :
6364 wrong += 1
64- print "\n Correct answer: %s" % answer
65+ print ( "\n Correct answer: %s" % answer )
6566
6667 finalizer (level ,questions ,correct ,wrong )
6768
@@ -72,13 +73,15 @@ def levels(level,questions):
7273
7374 global numbers
7475 numbers = []
75- while len (numbers ) <= 1 :
76- if level == 1 :
77- numbers .append (float (random .randint (1 ,15 )))
78- elif level == 2 :
79- numbers .append (float (random .randint (5 ,20 )))
80- elif level == 3 :
81- numbers .append (float (random .randint (10 ,30 )))
76+ if level == 1 :
77+ while len (numbers ) <= 1 :
78+ numbers .append (float (random .randint (1 ,15 )))
79+ elif level == 2 :
80+ while len (numbers ) <= 1 :
81+ numbers .append (float (random .randint (5 ,20 )))
82+ elif level == 3 :
83+ while len (numbers ) <= 1 :
84+ numbers .append (float (random .randint (10 ,30 )))
8285
8386 calculator (numbers ,operator ,level ,questions )
8487
@@ -98,7 +101,7 @@ def loop(level):
98101 questions += 1
99102
100103def difficultyselector ():
101- level = int (raw_input ("1. Easy\n 2.Medium\n 3. Hard\n \n " ))
104+ level = int (input ("1. Easy\n 2.Medium\n 3. Hard\n \n " ))
102105 if level == 1 :
103106 loop (level )
104107 elif level == 2 :
@@ -107,19 +110,7 @@ def difficultyselector():
107110 loop (level )
108111
109112def welcome ():
110- print "The Math Game\n "
113+ print ( "The Math Game\n Coded with <3 by Shreyas Daniel \n 2016 \n " )
111114 difficultyselector ()
112115
113116welcome ()
114-
115-
116- """
117- Working:
118- -> welcome(): main method, does what it says!
119- -> difficultyselector(): selects level
120- -> loop(level): a question looper based on your level from difficultyselector()
121- -> levels(level, questions): generates operator and operands
122- -> calculator(numbers,operator,level,questions): calculates the answer, increments right and wrong answers and prints wrong ones.
123- -> finalizer(level,questions,correct,wrong): evaluates solutions and sends them for result
124- -> result(correct, wrong, level): prints grade accordingly.
125- """
0 commit comments