Guessing Game
This Python code implements a simple number guessing game. Here’s a summary of how it works:
- Import Random Module: The code begins by importing the
randommodule, which is used to generate random numbers. - Define the
guess_number()Function: This function encapsulates the game logic. It generates a random number between 1 and 100 usingrandom.randint(1, 100). It initializes variables to track the number of attempts and whether the player has guessed the number correctly. - Welcome Message: The game starts by printing a welcome message and informing the player that the computer has chosen a number between 1 and 100.
- Game Loop: The main game loop continues until the player guesses the correct number. Within the loop, the player is prompted to enter their guess using
input(). The guess is converted to an integer usingint(). - Check Guess: The code checks whether the guess is correct. If it is, the player is congratulated, and the game ends. If the guess is incorrect, the code provides hints by informing the player whether their guess is too high or too low.
- Run the Game: Finally, the
guess_number()function is called to start the game.
This game is a basic example of user input, conditional statements, loops, and random number generation in Python. It can be expanded with additional features, such as a limited number of attempts, difficulty levels, or a graphical user interface.

import random
def guess_number():
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
# Initialize variables
attempts = 0
guessed = False
print("Welcome to the Number Guessing Game!")
print("I've chosen a number between 1 and 100. Can you guess it?")
# Main game loop
while not guessed:
guess = int(input("Enter your guess: "))
attempts += 1
Simple Calculator Code

# This function adds two numbers
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
