Python Codes Collection
Calculator with Loop Code
print("■ Smart Calculator ■")
while True:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Choose operation (+, -, *, /): ")
if op == '+':
print("Result =", num1 + num2)
elif op == '-':
print("Result =", num1 - num2)
elif op == '*':
print("Result =", num1 * num2)
elif op == '/':
if num2 != 0:
print("Result =", num1 / num2)
else:
print("Error: Division by Zero!")
else:
print("Invalid Operation!")
again = input("Do you want to calculate again? (yes/no): ")
if again.lower() != "yes":
break
Student Report Code
print("■ Student Report Card Generator ■")
name = input("Enter Student Name: ")
roll = input("Enter Roll Number: ")
subjects = ["Math", "Science", "English", "History", "Computer"]
marks = []
total = 0
pass_status = "Pass"
for subject in subjects:
score = int(input(f"Enter marks for {subject} (out of 100): "))
marks.append(score)
total += score
if score < 40:
pass_status = "Fail"
percentage = total / len(subjects)
if percentage >= 90:
grade = "A+"
elif percentage >= 75:
grade = "A"
elif percentage >= 60:
grade = "B"
elif percentage >= 40:
grade = "C"
else:
grade = "Fail"
if all(m >= 90 for m in marks):
topper = "■ Topper!"
else:
topper = ""
print("\n========= REPORT CARD =========")
print("Name :", name)
print("Roll No. :", roll)
print("-------------------------------")
for i in range(len(subjects)):
status = "Pass" if marks[i] >= 40 else "Fail"
print(f"{subjects[i]} : {marks[i]} ({status})")
print("-------------------------------")
print("Total Marks:", total, "/ 500")
print("Percentage :", round(percentage, 2), "%")
print("Grade :", grade)
print("Result :", pass_status, topper)
print("===============================")
Guessing Code
import random
print("Welcome to the Number Guessing Game!")
print("I am thinking of a number between 1 and 10")
secret_number = random.randint(1, 10)
for attempt in range(3):
guess = int(input("Enter your guess: "))
if guess == secret_number:
print("■ Congratulations! You guessed it right!")
break
elif guess < secret_number:
print("Too low! Try again.")
else:
print("Too high! Try again.")
else:
print("■ Sorry, you lost. The number was", secret_number)
Rock-Paper-Scissors Code
import random
print("■ Rock - Paper - Scissors Game ■")
print("Choose: rock, paper, or scissors")
user_choice = input("Your choice: ").lower()
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
print("Computer chose:", computer_choice)
if user_choice == computer_choice:
print("It's a Tie!")
elif (user_choice == "rock" and computer_choice == "scissors") or (user_choice == "paper" and co
print("■ You Win!")
else:
print("■ You Lose!")