Simple arithmetic game in Python Coming from a C/C++ background, this was my first python code I wrote to grasp a few basic concepts. The game involves giving random arithmetic operations to the users and scoring their responses. Player has 3 lives i.e he can mark 3 wrong answers at most. I've tried to time the game i.e the player gets 60 seconds to solve as many questions as possible. 

Any suggestions and improvements are welcome. 

import random
import time


# Function to generate expressions
from typing import List


def generate_expression(no_of_operators):
    operands = []
    operators = []
    operations = ['+', '-', '*', '/']
    expression = []
    operands_count = 0
    operators_count = 0
    for i in range(0, no_of_operators):
        operands.append(random.randint(0, 20))
    for i in range(0, no_of_operators - 1):
        operators.append((random.choice(operations)))
    for i in range(0, len(operators) + len(operands)):
        if i % 2 == 0:
            expression.append(operands[operands_count])
            operands_count += 1
        else:
            expression.append(operators[operators_count])
            operators_count += 1
    expression = ''.join(str(x) for x in expression)
    return expression


# Function to calculate the solution
def result(expression):
    return (int(eval(expression)))


# Function to evaluate if the answer is right
def evaluate(solution, user_solution):
    if solution == user_solution:
        return True
    else:
        return False



# Display Message

print("""Welcome to the maths game !!!
-----------------------------
Test your basic arithematic skills by playing this simple game. With every 5 correct answers, the level increase
increasing the difficulty of the questions.
Remember : 
----------
        1. Write only the integral part of the answer 
        2. Operator precedence applies
        3. You have 3 lives.
        4. Total of 60 seconds will be provided.
        5. The timer starts after the first answer is entered """)
input("Are you ready ?? Press any key to begin ! ")

# Variables on which the game operates
score = 0
level = 1
lives = 3
start = time.time()
finish_time = time.time() + 60  # for the timed mode, 60 seconds are needed

# While loop to drive the game
while lives != 0 and time.time() &lt; finish_time:
    # Increase the level of difficulty every 5 questions.
    if score != 0 and score % 5 == 0:
        level = level + 1
    print("LEVEL : ", level)
    no_of_operands = level + 1
    question_expression = generate_expression(no_of_operands)
    print(question_expression, end='')
    # Checking for any divide by zero or numerical errors that may show up
    correct_answer = 0
    try:
        correct_answer = result(question_expression)
    except:
        print("OOPS ! I messed up ! Lets do it again !")
        continue
    answer = int(input(" = "))

    if evaluate(correct_answer, answer):
        print("CORRECT ! ", end='')
        score = score + 1
        print("SCORE = ", score, "LIVES = ", lives)
    else:
        print("WRONG ! ", end='')
        lives = lives - 1
        print("SCORE = ", score, "LIVES = ", lives)
print("GAME OVER !!!")
print("Maximum Level = ", level, "SCORE = ", score)
Quick comment: If you have code which is supposed to run when you call the script (in your case its the "run the game" part of the code), its good practice to put it in a main() function and call the main function rather than keep all that code at the highest level of the file. At the bottom of the file, it would look something like this:

def main():
    functions_to_play_math_game()

if __name__ == "__main__":
    main()


When a python script runs, it gets a __name__ attribute depending on how it was called. If it was run through the command line (with $ python my_script.py), the __name__ attribute gets set to "__main__". So the second part of my code sample checks if the __name__ attribute indicates the script is being called to run, and if so, executes the code inside main().

The reason this is important is because when you import a module in python, it runs all the code inside that file. Now, if the file only contains function definitions, nothing out of the ordinary happens. But if you imported your file into another file, at the import my_file line your game would start running because there is no safeguard to prevent it from going. The catch is that when being imported, __name__ is not set to __main__ like it is when you call the script from a command line. So, if you have this safeguard at the bottom of your code, it wont run when its imported.