Welcome, everyone! In this series, you’ll find a variety of challenging exercises designed to spark your creativity. We encourage you to tackle these problems on your own first. Only seek help or consult the solutions if you’re stuck after 20 minutes. Enjoy the challenge!



Solutions

#1
print("Hello, World!")
Python

#2. Create a program that asks the user for their name and prints a greeting using that name.

user_name = str(input("Please enetr Your Name: "))
user_age = int(input("Please Enter Your Age: "))

print(f"Greeting {user_name} , i guess you are {user_age} old")
Python

#3. Write a program to calculate the area of a rectangle (length * width).
l = float(input("Please enter the lenght of the area: "))
w = float(input("Please enter the width of the area : "))
u = str(input("Please input the unit i.e. cm , mm , meter etc."))
area = l*w
print(f"The length of the rectangle is {l} {u} and the width of the rectangle is {w} {u} and the area of the rectangle is {area} ")
Python

#4. Create a program that converts Celsius to Fahrenheit.
def c_to_f(c):
    in_f = (0/5)*c+32
    print(f"{c} celcius will be {in_f} Fahrenheit")
    return " "

ask_temp = float(input("Please enter temprature in celcius: "))
print(c_to_f(ask_temp))
Python

#5. Write a program that takes two numbers as input and prints their sum.
# Program to take two numbers as input and print their sum

first_number = input("Please enter first number: ")
second_number = input("Please enter second number: ")

try:
    # Check if both inputs are numbers (either integer or float)
    first_number = float(first_number)
    second_number = float(second_number)
    
    # Calculate the sum
    sum_of_two = first_number + second_number
    
    # Print the sum
    print(f"The sum is {sum_of_two}")
    
except ValueError:
    print("Please enter only numbers (integers or floats)")
Python

#6. Create a program that checks if a number is even or odd.
even_or_odd = int(input("Please enter a number and i will tell you it is even or odd: "))
try:
    #Let us make sure that the input is an integer type 
    even_or_odd = int(even_or_odd)
    #let ius check if it is divisible by 2 or not 
    if even_or_odd %2 == 0:
        print(f"{even_or_odd} is even number ")
    elif even_or_odd %2 != 0:
        print(f"{even_or_odd} is odd number ")

except ValueError:
    print("Something went wrong ")
Python

# 6 but in diffrent way because The code is almost correct, but there's a small mistake. we're trying to convert
#the input to an integer twice, which is not necessary. Also, the try block should be around the int(input(...)) line,
#not the whole code.

# Program to check if a number is even or odd

try:
    even_or_odd = int(input("Please enter a number and I will tell you if it's even or odd: "))
except ValueError:
    print("Invalid input. Please enter a valid number.")
else:
    # Check if the number is divisible by 2
    if even_or_odd % 2 == 0:
        print(f"{even_or_odd} is an even number.")
    else:
        print(f"{even_or_odd} is an odd number.")
Python

#7. Write a program that prints all the numbers from 1 to 100.
l = []
for i in range(1,101):
    l.append(i)
print(f"the List is Ready: {l} ")

sum = 0
for each in l:
    sum = sum+each
print(sum)
Python

# additional way for exercise #7

try:
    num1 = int(input("Please enter starting range of the range: "))
    num2 = int(input("Please enter the ending range of the range: "))
    num2 = num2+1
except ValueError:
    print("Enter only Integers")
def sum_of_range(num1,num2):
    l = []
    for i in range(num1,num2):
        l.append(i)
    print (f"The list is ready {l}")
    sum = 0
    for x in l:
        sum+= x
    print(f"The Sum is {sum}")

print(sum_of_range(num1,num2))

# This code works but this is not the right way to do this because it is going to store the entir list in memory which is not good
Python

#8. Create a program that generates a random number between 1 and 10 and asks the user to guess it.

import random

def guess_number():
    # Generate a random number between 1 and 10
    number_to_guess = random.randint(1, 10)
    
    # Prompt the user to guess the number
    print("I'm thinking of a number between 1 and 10. Can you guess what it is?")
    
    # Initialize the guess
    user_guess = None
    
    # Loop until the user guesses correctly
    while user_guess != number_to_guess:
        try:
            # Get the user's guess
            user_guess = int(input("Enter your guess: "))
            
            # Check if the guess is correct
            if user_guess < 1 or user_guess > 10:
                print("Please enter a number between 1 and 10.")
            elif user_guess < number_to_guess:
                print("Too low! Try again.")
            elif user_guess > number_to_guess:
                print("Too high! Try again.")
            else:
                print("Congratulations! You guessed the number!")
        except ValueError:
            print("Invalid input. Please enter a number.")

# Call the function to start the game
guess_number()
Python

#9. Write a program that counts the number of vowels in a given string.

s = input("give me a sentence and i will count the vowels: ")
v = ['a', 'e', 'i', 'o', 'u']  # list
count = 0
for x in s.lower():
    if x in v:
        count += 1
print(f"there are {count} vowels")
Python

#10. Create a program that checks if a given year is a leap year.
y = int(input("Please enter a year and I will tell you if it is a leap year or not: "))

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

Leave a Reply

Your email address will not be published. Required fields are marked *