Number Guessing Game
import random
number = random.randint(1, 10)
guess = None
while guess != number:
guess = int(input("Guess a number between 1 and 10: "))
if guess < number:
print("Too Low")
elif guess > number:
print("Too High")
else:
print("Correct! The number was", number)
Simple Login System
stored_username = "admin"
stored_password = "1234"
username = input("Enter username: ")
password = input("Enter password: ")
if username == stored_username and password == stored_password:
print("Login successful")
else:
print("Invalid credentials")
Tic Tac Toe Game Using Dictionary
def print_board(board):
for row in [board[i*3:(i+1)*3] for i in range(3)]:
print("|".join(row))
def check_winner(board, player):
win_positions = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8),
(2,4,6)]
return any(board[i] == board[j] == board[k] == player for i,j,k in win_positions)
board = [" "]*9
current_player = "X"
for turn in range(9):
print_board(board)
move = int(input(f"{current_player}'s turn (0-8): "))
if board[move] == " ":
board[move] = current_player
if check_winner(board, current_player):
print_board(board)
print(f"{current_player} wins!")
break
current_player = "O" if current_player == "X" else "X"
else:
print("Invalid move, try again.")
else:
print("It's a draw!")
ATM Machine Simulation
balance = 1000
def display_menu():
print("\n1. Check Balance")
print("2. Deposit")
print("3. Withdraw")
print("4. Exit")
while True:
display_menu()
choice = input("Choose an option: ")
if choice == "1":
print(f"Balance: {balance}")
elif choice == "2":
amount = float(input("Enter amount to deposit: "))
balance += amount
print("Amount deposited.")
elif choice == "3":
amount = float(input("Enter amount to withdraw: "))
if amount <= balance:
balance -= amount
print("Amount withdrawn.")
else:
print("Insufficient balance.")
elif choice == "4":
print("Exiting...")
break
else:
print("Invalid choice.")
Simple Calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /): ")
if operation == "+":
print("Result:", num1 + num2)
elif operation == "-":
print("Result:", num1 - num2)
elif operation == "*":
print("Result:", num1 * num2)
elif operation == "/":
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Cannot divide by zero.")
else:
print("Invalid operation.")