Skip to content

Commit 4150ce8

Browse files
committed
add simple math quiz game tutorial
1 parent 1cff228 commit 4150ce8

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
106106
- [How to Change Text Color in Python](https://www.thepythoncode.com/article/change-text-color-in-python). ([code](general/printing-in-colors))
107107
- [How to Create a Watchdog in Python](https://www.thepythoncode.com/article/create-a-watchdog-in-python). ([code](general/directory-watcher))
108108
- [How to Convert Pandas Dataframes to HTML Tables in Python](https://www.thepythoncode.com/article/convert-pandas-dataframe-to-html-table-python). ([code](general/dataframe-to-html))
109+
- [How to Make a Simple Math Quiz Game in Python](https://www.thepythoncode.com/article/make-a-simple-math-quiz-game-in-python). ([code](general/simple-math-game))
109110

110111

111112

general/simple-math-game/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# [How to Make a Simple Math Quiz Game in Python](https://www.thepythoncode.com/article/make-a-simple-math-quiz-game-in-python)
2+
To run this:
3+
- `pip install -r requirements.txt`
4+
-
5+
```
6+
$ python simple_math_game.py
7+
8+
Round down to one Number after the Comma.
9+
When asked to press enter to continue, type stop to stop.
10+
11+
5 ** 4 = 625
12+
Correct!
13+
Points: 1
14+
Press "Enter" to continue
15+
16+
9 ** 18 = 190
17+
Wrong!
18+
Solution: 150094635296999121
19+
Points: 0
20+
Press "Enter" to continue
21+
22+
7 - 17 = -10
23+
Correct!
24+
Points: 1
25+
Press "Enter" to continue
26+
stop
27+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyinputplus
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Imports
2+
import pyinputplus as pyip
3+
from random import choice
4+
5+
# Variables
6+
questionTypes = ['+', '-', '*', '/', '**']
7+
numbersRange = [num for num in range(1, 20)]
8+
points = 0
9+
10+
# Hints
11+
print('Round down to one Number after the Comma.')
12+
print('When asked to press enter to continue, type stop to stop.\n')
13+
14+
# Game Loop
15+
while True:
16+
# Deciding and generating question
17+
currenType = choice(questionTypes)
18+
19+
promptEquation = str(choice(numbersRange)) + ' ' + currenType + ' ' + str(choice(numbersRange))
20+
solution = round(eval(promptEquation), 1)
21+
22+
# Getting answer from User
23+
answer = pyip.inputNum(prompt=promptEquation + ' = ')
24+
25+
# Feedback and Points
26+
if answer == solution:
27+
points += 1
28+
print('Correct!\nPoints: ',points)
29+
else:
30+
points -= 1
31+
print('Wrong!\nSolution: '+str(solution)+'\nPoints: ',points)
32+
33+
# Stopping the Game
34+
if pyip.inputStr('Press "Enter" to continue', blank=True) == 'stop':
35+
break
36+
37+
# Some Padding
38+
print('\n\n')

0 commit comments

Comments
 (0)