Make Quiz Game with Python | ninjasquad
In our previous Python tutorial, we have explained to Get Wifi Passwords using Python. In this tutorial, we will explain how to code some easy Game with Python.
Python is a popular programming language. It’s very easy to learn and any beginners can learn it to code within a short span of time. The beginners can easily code simple games using Python to expand their learning curve.
Here in this tutorial, we will code a text base Quiz Game in Python. It’s a simple Game without using any Python module.
So let’s start coding:
Develop Quiz Game
We will start with defining the variable totalQuestions
to define the total questions in Game. We will also define variable totalScore
for storing total score.
totalQuestions = 5 totalScore = 0
We will get user input if ready to play the game and store into answer
variable. We will check yes
if user is going to play.
answer = input('\nAre you ready to play the Quiz Game ? (yes/no) :\n') if answer.lower() == 'yes':
Then we will have first question of game and check for answer and display result as correct or wrong.
answer = input('\nQuestion 1: What is the most popular programming language?') if answer.lower() == 'python': totalScore += 1 print('correct') else: print('Wrong Answer :(')
Then we will have second question of game and check for answer and display result as correct or wrong.
answer = input('\nQuestion 2: Who is the creator of python?') if answer.lower() == 'guido van rossum': totalScore += 1 print('correct') else: print('Wrong Answer :(')
Then we will have third question of game and check for answer and display result as correct or wrong.
answer = input('\nQuestion 3: Python was first released in?') if answer.lower() == '1991': totalScore += 1 print('correct') else: print('Wrong Answer :(')
Then we will have fourth question of game and check for answer and display result as correct or wrong.
answer = input('\nQuestion 4: Python can be used on a server to create web applications? (yes/no)') if answer.lower() == 'yes': totalScore += 1 print('correct') else: print('Wrong Answer :(')
Then we will have fifth question of game and check for answer and display result as correct or wrong.
answer = input('\nQuestion 5: What is the correct file extension for Python files? ') if answer.lower() == '.py': totalScore += 1 print('correct') else: print('Wrong Answer :(')
Finally, we will calculate the marks obtain and display the result.
markObtain = (totalScore / totalQuestions) * 100 print('\nTotal marks obtained: ', markObtain)
Complete Code of Quiz Game
Here is complete code of our quiz game.
print('\n########## Welcome to Quiz Game! ###########\n') totalQuestions = 5 totalScore = 0 answer = input('\nAre you ready to play the Quiz Game ? (yes/no) :\n') if answer.lower() == 'yes': answer = input('\nQuestion 1: What is the most popular programming language?') if answer.lower() == 'python': totalScore += 1 print('correct') else: print('Wrong Answer :(') answer = input('\nQuestion 2: Who is the creator of python?') if answer.lower() == 'guido van rossum': totalScore += 1 print('correct') else: print('Wrong Answer :(') answer = input('\nQuestion 3: Python was first released in?') if answer.lower() == '1991': totalScore += 1 print('correct') else: print('Wrong Answer :(') answer = input('\nQuestion 4: Python can be used on a server to create web applications? (yes/no)') if answer.lower() == 'yes': totalScore += 1 print('correct') else: print('Wrong Answer :(') answer = input('\nQuestion 5: What is the correct file extension for Python files? ') if answer.lower() == '.py': totalScore += 1 print('correct') else: print('Wrong Answer :(') print('\nThank you for playing this quiz game, you attempted', totalScore, "questions correctly!") markObtain = (totalScore / totalQuestions) * 100 print('\nTotal marks obtained: ', markObtain) print('\nHave a good day!\n')
Output:
Are you ready to play the Quiz Game ? (yes/no) : yes Question 1: What is the most popular programming language?python correct Question 2: Who is the creator of python?guido van rossum correct Question 3: Python was first released in?1991 correct Question 4: Python can be used on a server to create web applications? (yes/no)yes correct Question 5: What is the correct file extension for Python files? .py correct Thank you for playing this quiz game, you attempted 5 questions correctly! Total marks obtained: 100.0 Have a good day!
Source: Internet