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

Assignments 2 OS

The document contains multiple Python programming exercises, including functions for calculating grades based on user input, printing numbers while excluding those divisible by 5 and 7, summing digits of a number, removing duplicates from a list, counting character occurrences in a string, and creating a BankAccount class with deposit and withdrawal methods. Each exercise includes a complete code solution and handles user input and error checking. The document serves as a practical guide for implementing various programming concepts in Python.

Uploaded by

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

Assignments 2 OS

The document contains multiple Python programming exercises, including functions for calculating grades based on user input, printing numbers while excluding those divisible by 5 and 7, summing digits of a number, removing duplicates from a list, counting character occurrences in a string, and creating a BankAccount class with deposit and withdrawal methods. Each exercise includes a complete code solution and handles user input and error checking. The document serves as a practical guide for implementing various programming concepts in Python.

Uploaded by

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

‫حازم ابو هاشم محمد ‪Name:‬‬

‫‪Sec: 4‬‬
‫‪Id: 323240115‬‬
1)Write python program using method that takes from user no of courses and degrees of courses (0:
100) if userf enter degree greater than 100 or less than zero, print message must degree

is (0 : 100) then enter again degree.

then print (A, B,C,D,E,F) for each course.

degree>= 90 : Grade A

degree>= 80 : Grade B

degree>= 70 : Grade C

degree>= 60 : Grade D

degree>= 40 : Grade E

degree< 40 : Grade F

then print total and (A, B,C,D,E,F) based on percentage of total of all courses.

Percentage >= 90% : Evaluation A

Percentage >= 80% : Evaluation B

Percentage >= 70% : Evaluation C

Percentage >= 60% : Evaluation D

Percentage >= 40% : Evaluation E

Percentage < 40% : Evaluation F

Answer

def get_degree_input(course_num):

while True:

try:

degree = float(input(f"Enter degree for course {course_num + 1} (0 - 100): "))

if 0 <= degree <= 100:

return degree

else:

print("Degree must be between 0 and 100. Please enter again.")

except ValueError:

print("Invalid input. Please enter a numeric value.")


def calculate_grade(degree):

if degree >= 90:

return 'A'

elif degree >= 80:

return 'B'

elif degree >= 70:

return 'C'

elif degree >= 60:

return 'D'

elif degree >= 40:

return 'E'

else:

return 'F'

def main():

try:

num_courses = int(input("Enter number of courses: "))

if num_courses <= 0:

print("Number of courses must be greater than zero.")

return

degrees = []

for i in range(num_courses):

degree = get_degree_input(i)

degrees.append(degree)

print(f"Grade for course {i + 1}: {calculate_grade(degree)}")

total = sum(degrees)

percentage = (total / (num_courses * 100)) * 100


print(f"\nTotal Degrees: {total}")

print(f"Overall Percentage: {percentage:.2f}%")

print(f"Overall Evaluation: {calculate_grade(percentage)}")

except ValueError:

print("Invalid input. Please enter a numeric value for the number of courses.")

if __name__ == "__main__":

main()

2) Write python program to print numbers from 1 : 100 but not divide by 7 and 5

Answer

def print_numbers():

for num in range(1, 101):

if num % 7 != 0 and num % 5 != 0:

print(num)

if __name__ == "__main__":

print_numbers()

3) Write python program to print digits and sum of digits for any number using method

Answer

def print_digits_and_sum(number):

# Convert the number to string to iterate over each digit

digits = str(number)

sum_of_digits = 0

print("Digits in the number:")


for digit in digits:

print(digit) # Print each digit

sum_of_digits += int(digit) # Sum the digits

print(f"Sum of digits: {sum_of_digits}")

def main():

try:

number = int(input("Enter any number: "))

print_digits_and_sum(number)

except ValueError:

print("Invalid input. Please enter a numeric value.")

if __name__ == "__main__":

main()

4) Python Program to Remove Duplicate Element From a List

Answer

def remove_duplicates(input_list):

seen = set() # Create a set to track seen elements

result = [] # List to hold unique elements

for item in input_list:

if item not in seen:

seen.add(item) # Add to seen set

result.append(item) # Append unique item to result

return result
def main():

# Sample input list with duplicates

input_list = [1, 2, 3, 2, 4, 5, 1, 6, 4, 7]

print("Original list:", input_list)

# Remove duplicates

unique_list = remove_duplicates(input_list)

print("List after removing duplicates:", unique_list)

if __name__ == "__main__":

main()

5) Python Program to Count the Number of Occurrence of a Character in String

Answer

def count_character_occurrences(input_string, character):

count = 0 # Initialize count

for char in input_string:

if char == character:

count += 1 # Increment count if character matches

return count

def main():

input_string = input("Enter a string: ")

character = input("Enter the character to count: ")

if len(character) != 1:

print("Please enter a single character.")

return
occurrences = count_character_occurrences(input_string, character)

print(f"The character '{character}' occurs {occurrences} times in the string.")

if __name__ == "__main__":

main()

6) Create a BankAccount class with attributes like account_number, balance, and owner_name.

. Add methods to deposit, withdraw, and check the balance.


. Implement a method to display the account information.
Answer

class BankAccount:

def __init__(self, account_number, owner_name, balance=0):

self.account_number = account_number

self.owner_name = owner_name

self.balance = balance

def deposit(self, amount):

if amount > 0:

self.balance += amount

print(f"Deposited: ${amount:.2f}")

else:

print("Deposit amount must be positive.")

def withdraw(self, amount):

if 0 < amount <= self.balance:

self.balance -= amount

print(f"Withdrew: ${amount:.2f}")
elif amount > self.balance:

print("Insufficient funds.")

else:

print("Withdrawal amount must be positive.")

def check_balance(self):

print(f"Current balance: ${self.balance:.2f}")

def display_account_info(self):

print(f"Account Number: {self.account_number}")

print(f"Owner Name: {self.owner_name}")

print(f"Balance: ${self.balance:.2f}")

# Example usage

if __name__ == "__main__":

account = BankAccount("12345678", "John Doe", 1000)

account.display_account_info()

account.deposit(500)

account.check_balance()

account.withdraw(200)

account.check_balance()

account.withdraw(2000) # Should show insufficient funds

account.display_account_info()

You might also like