Wednesday, January 17, 2024

Simple Python Programs using if and if else

 # Program to check if a number is positive or negative


num = float(input("Enter a number: "))


if num > 0:

    print("The number is positive.")

elif num == 0:

    print("The number is zero.")

else:

    print("The number is negative.")


# Program to check if a number is even or odd


num = int(input("Enter a number: "))

if num % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")


# Program to find the maximum of three numbers


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

if num1 >= num2 and num1 >= num3:
    print("The maximum number is:", num1)
elif num2 >= num1 and num2 >= num3:
    print("The maximum number is:", num2)
else:
    print("The maximum number is:", num3)


# Program to check eligibility to vote


age = int(input("Enter your age: "))

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")


# Program to calculate grades based on marks


marks = float(input("Enter your marks: "))

if marks >= 90:
    grade = 'A'
elif 80 <= marks < 90:
    grade = 'B'
elif 70 <= marks < 80:
    grade = 'C'
elif 60 <= marks < 70:
    grade = 'D'
else:
    grade = 'F'

print(f"Your grade is: {grade}")

# Program to check if a year is a leap year


year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

# Program to implement a simple calculator


num1 = float(input("Enter the first number: "))
operator = input("Enter an operator (+, -, *, /): ")
num2 = float(input("Enter the second number: "))

if operator == '+':
    result = num1 + num2
elif operator == '-':
    result = num1 - num2
elif operator == '*':
    result = num1 * num2
elif operator == '/':
    if num2 != 0:
        result = num1 / num2
    else:
        result = "Error: Division by zero"
else:
    result = "Error: Invalid operator"

print(f"Result: {result}")


# Program to check if a string is a palindrome


def is_palindrome(s):
    s = s.lower()
    reversed_s = s[::-1]
    
    if s == reversed_s:
        print("The string is a palindrome.")
    else:
        print("The string is not a palindrome.")

user_input = input("Enter a string: ")
is_palindrome(user_input)


# Program for a simple guess the number game


import random

secret_number = random.randint(1, 10)

guess = int(input("Guess the secret number (between 1 and 10): "))

if guess == secret_number:
    print("Congratulations! You guessed the correct number.")
else:
    print(f"Sorry, the correct number was {secret_number}. Try again.")


# Program to convert temperature from Celsius to Fahrenheit or vice versa


def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
    return (fahrenheit - 32) * 5/9

choice = input("Choose conversion (C to F or F to C): ").upper()

if choice == 'C':
    celsius = float(input("Enter temperature in Celsius: "))
    result = celsius_to_fahrenheit(celsius)
    print(f"{celsius}°C is equal to {result}°F.")
elif choice == 'F':
    fahrenheit = float(input("Enter temperature in Fahrenheit: "))
    result = fahrenheit_to_celsius(fahrenheit)
    print(f"{fahrenheit}°F is equal to {result}°C.")
else:
    print("Invalid choice. Please enter 'C' or 'F'.")


# Program to check if a number is prime


def is_prime(number):
    if number <= 1:
        return False
    for i in range(2, int(number**0.5) + 1):
        if number % i == 0:
            return False
    return True

num = int(input("Enter a number: "))

if is_prime(num):
    print(f"{num} is a prime number.")
else:
    print(f"{num} is not a prime number.")

# Program to find the factorial of a number


def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

num = int(input("Enter a number: "))
result = factorial(num)
print(f"The factorial of {num} is {result}.")


# Program for a simple Rock, Paper, Scissors game


import random

choices = ["Rock", "Paper", "Scissors"]

user_choice = input("Enter your choice (Rock, Paper, Scissors): ").capitalize()
computer_choice = random.choice(choices)

print(f"Computer's choice: {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 computer_choice == "Rock") or
    (user_choice == "Scissors" and computer_choice == "Paper")
):
    print("You win!")
else:
    print("You lose!")

No comments:

Post a Comment

Input Text and Display in tkinter

  import tkinter as tk def display_text():     text = entry.get()     label.config(text="You entered: " + text) root = tk.Tk() roo...