# Basic Python Programming Challenges

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to write a solution for a challenge.

## Challenge

We have to generate a random set of basic arithmetic operations. The user will give the number of questions, and we have to generate questions. After every question, the user will answer it. At the end of the program, we have to give the score. Let's try it.

### Example

# importing random and operator modules
import random
import operator
# main function
# taking number of questions that we have to generate
def main(n):
print("Welcome to the quiz\nYou should answer floats upto 2 decimals")
# initialising score to 0
score = 0
# loop to generate n questions
for i in range(n):
# getting answer and correctness of a question
# checking whether is_correct is True or not
if is_correct:
# increment score by 1 if correct
score += 1
print('Correct Congrats!')
else:
# displaying the total score
print(f'Total score: {score}')
# function for the question
def question(n):
# getting answer from the generate_function
# taking answer from the user
# returning answer to the main function
# function to generate a random question
def generate_question():
# initialising operators for random generation
operators = {
'-' : operator.sub,
'*' : operator.mul,
'/' : operator.truediv,
'//' : operator.floordiv,
'%' : operator.mod
}
# initialising numbers for expressions
nums = [i for i in range(10)]
# getting two random numbers from nums for calculation
_1, _2 = nums[random.randint(0, 9)], nums[random.randint(0, 9)]
# generating random operator from the list of operators
symbol = list(operators.keys())[random.randint(0, 5)]
print(f'{_1} {symbol} {_2}?')
if __name__ == '__main__':
main(5)

### Output

If you run the above code, you will get the following results.

Welcome to the quiz
You should answer floats upto 2 decimals
5 + 7?
Correct Congrats!
9 / 9?
Correct Congrats!
4 + 7?
Correct Congrats!
6 // 6?
Total score: 5