0% found this document useful (0 votes)
4 views

Python Assignment Solution

The document contains solutions for six Python programming tasks, including arithmetic operations, temperature conversion, simple interest calculation, palindrome checking, a number guessing game, and file handling. Each task is implemented as a function that prompts user input and performs the respective operations. The solutions include error handling for invalid inputs and specific conditions like division by zero.

Uploaded by

Zameer Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Assignment Solution

The document contains solutions for six Python programming tasks, including arithmetic operations, temperature conversion, simple interest calculation, palindrome checking, a number guessing game, and file handling. Each task is implemented as a function that prompts user input and performs the respective operations. The solutions include error handling for invalid inputs and specific conditions like division by zero.

Uploaded by

Zameer Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Assignment Solutions

Task 1: Arithmetic Operations


# Arithmetic Operations Program
def arithmetic_operations():
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print(f"Addition: {num1 + num2}")


print(f"Subtraction: {num1 - num2}")
print(f"Multiplication: {num1 * num2}")
if num2 != 0:
print(f"Division: {num1 / num2}")
else:
print("Division by zero is not allowed!")
except ValueError:
print("Invalid input! Please enter numeric values.")

arithmetic_operations()

Task 2: Temperature Converter


# Temperature Converter Program
def convert_temperature():
try:
choice = int(input("Choose conversion type (1: C to F, 2: F to C): "))
if choice == 1:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"Temperature in Fahrenheit: {fahrenheit:.2f}°F")
elif choice == 2:
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print(f"Temperature in Celsius: {celsius:.2f}°C")
else:
print("Invalid choice!")
except ValueError:
print("Invalid input! Please enter numeric values.")

convert_temperature()
Task 3: Simple Interest Calculator
# Simple Interest Calculator
def calculate_simple_interest():
try:
P = float(input("Enter Principal Amount: "))
R = float(input("Enter Rate of Interest: "))
T = float(input("Enter Time (in years): "))
S = (P * R * T) / 100
print(f"Simple Interest: {S}")
except ValueError:
print("Invalid input! Please enter numeric values.")

calculate_simple_interest()

Task 4: Palindrome Checker


# Palindrome Checker
def is_palindrome():
text = input("Enter a string: ").lower().replace(" ", "")
if text == text[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

is_palindrome()

Task 5: Number Guessing Game


# Number Guessing Game
import random

def number_guessing_game():
secret_number = random.randint(1, 100)
attempts = 0
while True:
try:
guess = int(input("Guess the number (1-100): "))
attempts += 1
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Correct! You guessed it in {attempts} attempts.")
break
except ValueError:
print("Invalid input! Please enter a number.")

number_guessing_game()

Task 6: Save and Read a File


# File Handling: Save and Read a File
def save_and_read_file():
file_name = "user_text.txt"

# Writing to the file


with open(file_name, "w") as file:
for i in range(5):
file.write(input(f"Enter line {i+1}: ") + "\n")

# Reading from the file


print("\nFile Contents:")
with open(file_name, "r") as file:
print(file.read())

save_and_read_file()

You might also like