diff --git a/FunProjects/Grades.py b/FunProjects/Grades.py index 21ae916..8070efe 100644 --- a/FunProjects/Grades.py +++ b/FunProjects/Grades.py @@ -3,16 +3,22 @@ def __init__(self): self.grades = {} def add_grade(self, student, grade): - self.grades[student] = grade - print(student,"Grade is ",self.grades[student]) + if isinstance(grade, (int, float)): + self.grades[student] = grade + print(student, "Grade is", self.grades[student]) + else: + print(f"Invalid grade for {student}. Grade must be a number.") + def calculate_average(self): + if not self.grades: + return 0 # Avoid division by zero total = sum(self.grades.values()) average = total / len(self.grades) return average -# Example Usage +# Example Usage ! grades = StudentGrades() grades.add_grade("John", 85) grades.add_grade("Sarah", 90) grades.add_grade("Mike", 78) -print("Average grade:", grades.calculate_average()) \ No newline at end of file +print("Average grade:", grades.calculate_average()) diff --git a/problems/easy/easy_q13.py b/problems/easy/easy_q13.py index dd23d02..1370cfa 100644 --- a/problems/easy/easy_q13.py +++ b/problems/easy/easy_q13.py @@ -1,6 +1,5 @@ -# Month Name: Write a program that takes a number (1-12) and prints the corresponding month name using a switch case. def month_name(month): - switch = { + months = { 1: "January", 2: "February", 3: "March", @@ -14,12 +13,14 @@ def month_name(month): 11: "November", 12: "December" } - - return switch.get(month) + + return months.get(month, "Invalid month number") + if __name__ == "__main__": - chooseMonthNum = int(input("Enter the Month from 1 to 12: ")) - - - return switch.get(month, "Invalid Month") + try: + chooseMonthNum = int(input("Enter the Month from 1 to 12: ")) + print("Month is:", month_name(chooseMonthNum)) + except ValueError: + print("Invalid input. Please enter a number.") \ No newline at end of file diff --git a/problems/easy/easy_q14.py b/problems/easy/easy_q14.py index 8f09e9d..5ff0ec9 100644 --- a/problems/easy/easy_q14.py +++ b/problems/easy/easy_q14.py @@ -1,29 +1,18 @@ -# Vowel or Consonant: Accept a character and use a switch case to determine if it is a vowel or a consonant. def vowel_or_consonant(char): - char=char.lower() - switch = { - 'a': "Vowel", - 'e': "Vowel", - 'i': "Vowel", - 'o': "Vowel", - 'u': "Vowel", - 'A': "Vowel", - 'E': "Vowel", - 'I': "Vowel", - 'O': "Vowel", - 'U': "Vowel", - } - - if char in switch: - return "Vowels" + char = char.lower() + vowels = {'a', 'e', 'i', 'o', 'u'} + + if len(char) != 1 or not char.isalpha(): + return "Invalid input. Please enter a single alphabet letter." + + if char in vowels: + return "Vowel" else: return "Consonant" + if __name__ == "__main__": - characterInput = input("Enter the character : ") - - return switch.get(char) + characterInput = input("Enter a single alphabet character: ") + result = vowel_or_consonant(characterInput) + print("Result:", result) - - - return switch.get(char, "constant") - \ No newline at end of file + \ No newline at end of file diff --git a/problems/easy/easy_q15.py b/problems/easy/easy_q15.py index 09bbf29..a91c636 100644 --- a/problems/easy/easy_q15.py +++ b/problems/easy/easy_q15.py @@ -1,31 +1,17 @@ -# Grade Description: Write a program that accepts a grade (A, B, C, D, F) and prints its description (e.g., A = Excellent, B = Good, etc.) using a switch case. def grade_description(grade): switch = { - 'A': "Excellent", 'B': "Good", 'C': "Average", - 'D': "Poor", - 'B': "Good", - 'C': "Average", 'D': "Poor", - 'A': "Excellent", - 'F': "Fail" - - } - - return switch.get(grade, "Not a valid grade") + return switch.get(grade, "Not a valid grade") if __name__ == "__main__": - - user = input("Enter grade among A,B,C,D,F *in captial:") - rs = grade_description(user) - - alp = input("Enter a grade: ").upper() - rs = grade_description(alp) - - print(rs) + grade_input = input("Enter a grade (A, B, C, D, F) in capital letters: ").upper() + result = grade_description(grade_input) + print(result) + \ No newline at end of file