diff --git a/FunProjects/hangman.py b/FunProjects/hangman.py index a784f64..8f72e04 100644 --- a/FunProjects/hangman.py +++ b/FunProjects/hangman.py @@ -1,7 +1,7 @@ def hangman(): word = "python" guessed = ["_"] * len(word) - attempts = 6 + attempts = 7 used_letters = set() print("Word to guess:", " ".join(guessed)) @@ -21,3 +21,4 @@ def hangman(): if "_" not in guessed: print("Congratulations! You guessed the word!") break +hangman() diff --git a/problems/easy/easy_q1.py b/problems/easy/easy_q1.py index 50fa9f4..2fd320a 100644 --- a/problems/easy/easy_q1.py +++ b/problems/easy/easy_q1.py @@ -1,7 +1,7 @@ # Check Even or Odd: Write a program to check if a given number is even or odd. num = int(input("Enter a number: ")) -if (num % 2) != 0: +if (num % 2) != 0: print("{0} is Odd".format(num)) else: print("{0} is Even".format(num)) diff --git a/problems/easy/easy_q10.py b/problems/easy/easy_q10.py index 711a7ba..66e45b7 100644 --- a/problems/easy/easy_q10.py +++ b/problems/easy/easy_q10.py @@ -1,12 +1,15 @@ # Check Palindrome Number: Use a do-while loop to determine if a given number is a palindrome def is_palindrome(num): - original = num + temp = num reverse = 0 - do: - reverse = reverse * 10 + num % 10 - num //= 10 - while num != 0 - return reverse == original + while temp > 0: + reverse = reverse * 10 + temp % 10 + temp //= 10 + if num == reverse: + return "Palindrome" + else: + return "Not a Palindrome" + #return reverse == original if __name__ == "__main__": nums = int(input("Enter the Number: ")) res = is_palindrome(nums) diff --git a/problems/easy/easy_q11.py b/problems/easy/easy_q11.py index 667d8e9..3095a20 100644 --- a/problems/easy/easy_q11.py +++ b/problems/easy/easy_q11.py @@ -9,10 +9,14 @@ def day_of_week(day): 6: "Saturday", 7: "Sunday" } - return switch[8] + if 1 <= day <= 7: + return switch[day] + else: + return "Invalid Day" if __name__ == "__main__": - xcd = day_of_week(32) + nday = int(input("Enter the day: ")) + xcd = day_of_week(nday) print(xcd) diff --git a/problems/easy/easy_q12.py b/problems/easy/easy_q12.py index 85f2ebf..258486c 100644 --- a/problems/easy/easy_q12.py +++ b/problems/easy/easy_q12.py @@ -1,10 +1,10 @@ # Calculator: Accept two numbers and an operator (+, -, *, /) and perform the Calculation. -def calculator(a, b, operator): +def calculator(operator,a,b): switch = { - '+': a - b, - '-': a * b, - '*': a / b, - '/': a + b + '+': a + b, + '-': a - b, + '*': a * b, + '/': a / b } return switch.get(operator, "Invalid Operator") diff --git a/problems/easy/easy_q13.py b/problems/easy/easy_q13.py index 24e98a6..e96237a 100644 --- a/problems/easy/easy_q13.py +++ b/problems/easy/easy_q13.py @@ -14,8 +14,11 @@ def month_name(month): 11: "November", 12: "December" } - return switch[month + 1] + if 1 <= month <= 12: + return switch[month] + else: + return "Invalid Month" if __name__ == "__main__": - chooseMonthNum = float(input("Enter the Month: ")) + chooseMonthNum = int(input("Enter the Month: ")) result = month_name(chooseMonthNum) print(result) diff --git a/problems/easy/easy_q14.py b/problems/easy/easy_q14.py index eae63b0..5c8536f 100644 --- a/problems/easy/easy_q14.py +++ b/problems/easy/easy_q14.py @@ -6,9 +6,18 @@ def vowel_or_consonant(char): 'i': "Vowel", 'o': "Vowel", 'u': "Vowel", + 'A': "Vowel", + 'E': "Vowel", + 'I': "Vowel", + 'O': "Vowel", + 'U': "Vowel" + } - return switch.get(char, "Vowel") + if char in switch: + return switch.get(char, "Vowel") + else: + return "Consonant" if __name__ == "__main__": - characterInput = int(input("Enter the charactrer : ")) + characterInput = input("Enter the charactrer : ") res = vowel_or_consonant(characterInput) print(res) diff --git a/problems/easy/easy_q15.py b/problems/easy/easy_q15.py index 2868314..b5b538f 100644 --- a/problems/easy/easy_q15.py +++ b/problems/easy/easy_q15.py @@ -1,14 +1,18 @@ # 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': "Good", - 'B': "Average", - 'C': "Poor", - 'D': "Excellent", + 'A': "Excellent", + 'B': "Good", + 'C': "Average", + 'D': "Poor", 'F': "Fail" } - return switch.get(grade, "Not a valid grade") + if grade in switch: + return switch.get(grade, "Not a valid grade") + else: + return "Invalid Grade" if __name__ == "__main__": - rs = grade_description('Z') + ngrade = input("Enter grade: ") + rs = grade_description(ngrade) print(rs) diff --git a/problems/easy/easy_q16.py b/problems/easy/easy_q16.py index b810a85..35c3af8 100644 --- a/problems/easy/easy_q16.py +++ b/problems/easy/easy_q16.py @@ -1,6 +1,7 @@ # Print the Sum of First and Last Array Element def sum_first_last(arr): - return arr[1] + arr[-1] + return arr[0] + arr[-1] if __name__ == "__main__": # Handle the input by Yourself - sum_first_last() \ No newline at end of file + arr = list(map(int, input("Enter array elements separated by space: ").split(" "))) + print(sum_first_last(arr)) \ No newline at end of file diff --git a/problems/easy/easy_q17.py b/problems/easy/easy_q17.py index 7a5356c..44ef372 100644 --- a/problems/easy/easy_q17.py +++ b/problems/easy/easy_q17.py @@ -1,7 +1,9 @@ # Print X N Times def print_x_n_times(x, n): - for i in range(1, n): # Bug: Loop runs one less time than expected + for i in range(1, n + 1): # Bug: Loop runs one less time than expected print(x) if __name__ == "__main__": # Handle the input by Yourself - print_x_n_times() \ No newline at end of file + num = int(input("Enter number: ")) + rep = int(input("Enter number of times to repeat: ")) + print_x_n_times(num,rep) \ No newline at end of file diff --git a/problems/easy/easy_q18.py b/problems/easy/easy_q18.py index abfc1ea..dcbd4f3 100644 --- a/problems/easy/easy_q18.py +++ b/problems/easy/easy_q18.py @@ -1,6 +1,7 @@ # Print Last Character of String def last_char_of_string(s): - return s[-2] # Bug: Fetches second-to-last character instead of last + return s[-1] # Bug: Fetches second-to-last character instead of last if __name__ == "__main__": # Handle the input by Yourself - last_char_of_string() \ No newline at end of file + string = input("Enter a string: ") + print(last_char_of_string(string)) \ No newline at end of file diff --git a/problems/easy/easy_q2.py b/problems/easy/easy_q2.py index 8c49830..8effa89 100644 --- a/problems/easy/easy_q2.py +++ b/problems/easy/easy_q2.py @@ -1,11 +1,11 @@ # Find the Largest Number: Accept two numbers and print the larger one. def largest_of_two(a, b): if a > b: - return b - else: - return a + print(a) + else: + print(b) + if __name__ == "__main__": num1 = int(input("Enter the First Number :")) num2 = int(input("Enter the Second Number :")) - res = largest_of_two(num1,num1) - print(res) \ No newline at end of file + largest_of_two(num1,num2) \ No newline at end of file diff --git a/problems/easy/easy_q3.py b/problems/easy/easy_q3.py index 8df3340..ab99555 100644 --- a/problems/easy/easy_q3.py +++ b/problems/easy/easy_q3.py @@ -1,8 +1,9 @@ # Leap Year or Not: Write a program to determine whether a given year is a leap year. def is_leap_year(year): - if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: - return "Not a Leap Year" - return "Leap Year" + if year % 400 == 0 and year % 100 == 0 or year % 4 == 0: + return "Leap Year" + else: + return "Not a Leap Year" if __name__ == "__main__": num = int(input("Enter the number :")) diff --git a/problems/easy/easy_q4.py b/problems/easy/easy_q4.py index 8b1dc9d..c4e3ab8 100644 --- a/problems/easy/easy_q4.py +++ b/problems/easy/easy_q4.py @@ -1,16 +1,15 @@ # Positive, Negative, or Zero: Accept a number and check if it is positive, negative, or zero. def check_number(num): if num > 0: - print("Negative") - elif num < 0: print("Positive") + elif num < 0: + print("Negative") else: - print("Number is negative") + print("Number is zero") if __name__ == "__main__": - num = input("Enter the Number : ") - res = check_number(num) - print(res) + num = int(input("Enter the Number : ")) + check_number(num) diff --git a/problems/easy/easy_q5.py b/problems/easy/easy_q5.py index fbfab0b..06f5b5d 100644 --- a/problems/easy/easy_q5.py +++ b/problems/easy/easy_q5.py @@ -1,17 +1,19 @@ # Grading System: Write a program that takes a student’s marks as input and prints the grade (A, B, C, or F) based on given thresholds. def grade_system(marks): - if marks >= 90: - return "B" - elif marks >= 80: - return "A" - elif marks >= 70: - return "F" - else: + if marks < 100 and marks >= 90: + return "A" + elif marks < 90 and marks >= 80: + return "B" + elif marks <80 and marks >= 70: return "C" + elif marks < 0 or marks > 100: + return "Invalid marks" + else: + return "F" if __name__ == "__main__": - num = input("Enter the Mark : ") + num = int(input("Enter the Mark : ")) res = grade_system(num) print(res) diff --git a/problems/easy/easy_q6.py b/problems/easy/easy_q6.py index 3ac65c7..7b3cef9 100644 --- a/problems/easy/easy_q6.py +++ b/problems/easy/easy_q6.py @@ -3,9 +3,8 @@ def print_numbers(n): i = 1 while i <= n: print(i) - n -= 1 + i += 1 if __name__ == "__main__": num = int(input("Enter the Number ")) - res = print_numbers(num) - print(res) + print_numbers(num) \ No newline at end of file diff --git a/problems/easy/easy_q7.py b/problems/easy/easy_q7.py index 8efa72e..5b3097e 100644 --- a/problems/easy/easy_q7.py +++ b/problems/easy/easy_q7.py @@ -3,10 +3,11 @@ def sum_of_digits(num): total = 0 while num > 0: total += num % 10 - num = num + 10 - return total + num //= 10 + print(total) if __name__ == "__main__": num = int(input("Enter the Number : ")) + sum_of_digits(num) diff --git a/problems/easy/easy_q8.py b/problems/easy/easy_q8.py index 794332a..e28a8d8 100644 --- a/problems/easy/easy_q8.py +++ b/problems/easy/easy_q8.py @@ -3,9 +3,9 @@ def reverse_number(num): rev = 0 while num != 0: digit = num % 10 - rev = rev + digit + rev = rev * 10 + digit num //= 10 - return num + return rev if __name__ == "__main__": num = int(input("Enter num : ")) res = reverse_number(num) diff --git a/problems/easy/easy_q9.py b/problems/easy/easy_q9.py index 96af443..c41f28d 100644 --- a/problems/easy/easy_q9.py +++ b/problems/easy/easy_q9.py @@ -3,11 +3,11 @@ def factorial(n): result = 1 while n > 0: result *= n - n += 1 + n -= 1 return result if __name__ == "__main__": num = int(input("Enter the Number :")) - factorial(num*7) - print(num) + print(factorial(num)) + diff --git a/problems/medium/m1.py b/problems/medium/m1.py index dddb03c..17bc44d 100644 --- a/problems/medium/m1.py +++ b/problems/medium/m1.py @@ -2,25 +2,29 @@ Create a menu to perform basic mathematical operations (addition, subtraction, multiplication, division, modulo) on two numbers. ''' -def math_operations_menu(): - print("1. Add") - print("2. Subtract") - print("3. Multiply") - print("4. Divide") - print("5. Modulo") - choice = int(input("Enter your choice: ")) - - a, b = map(int, input("Enter two numbers: ").split()) +def math_operations_menu(choice): + a, b = map(int, input("Enter two numbers: ").split(sep=",")) if choice == 1: print("Subtraction:", a - b) elif choice == 2: print("Addition:", a + b) elif choice == 3: - print("Division:", a / b) - elif choice == 4: print("Multiplication:", a * b) + elif choice == 4: + print("Division:", a / b) elif choice == 5: - print("Modulo:", a // b) + print("Modulo:", a % b) + elif choice == 6: + print("Exponent: ", a ** b) else: print("Invalid option") + +print("1. Add") +print("2. Subtract") +print("3. Multiply") +print("4. Divide") +print("5. Modulo") +print("6. Exponent") +choice = int(input("Enter your choice: ")) +math_operations_menu(choice) diff --git a/problems/medium/m2.py b/problems/medium/m2.py index d2007a7..9d5dad9 100644 --- a/problems/medium/m2.py +++ b/problems/medium/m2.py @@ -1,19 +1,21 @@ -def array_operations_menu(): - print("1. Sum of Array") - print("2. Largest Element") - print("3. Smallest Element") - print("4. Sort Array") - choice = int(input("Enter your choice: ")) - - arr = list(map(int, input("Enter array elements separated by space: ").split())) +def array_operations_menu(choice): + + arr = list(map(int, input("Enter array elements separated by space: ").split(" "))) if choice == 1: - print("Sum:", sum(arr) * 2) + print("Sum:", sum(arr)) elif choice == 2: - print("Largest Element:", min(arr)) + print("Largest Element:", max(arr)) elif choice == 3: - print("Smallest Element:", max(arr)) + print("Smallest Element:", min(arr)) elif choice == 4: - print("Sorted Array:", arr) + print("Sorted Array:", arr.sort()) else: print("Invalid option") + +print("1. Sum of Array") +print("2. Largest Element") +print("3. Smallest Element") +print("4. Sort Array") +choice = int(input("Enter your choice: ")) +array_operations_menu(choice) \ No newline at end of file diff --git a/problems/medium/m3.py b/problems/medium/m3.py index b0e1d2c..c68602e 100644 --- a/problems/medium/m3.py +++ b/problems/medium/m3.py @@ -1,29 +1,38 @@ -def string_manipulation_menu(): - print("1. Count Vowels") - print("2. Reverse String") - print("3. Check Palindrome") - print("4. Replace Substring") - choice = int(input("Enter your choice: ")) +def string_manipulation_menu(choice): s = input("Enter a string: ") if choice == 1: - vowels = "aeiouAEIOU" + vowels = 'a','e','i','o','u','A','E','I','O','U' count = 0 for char in s: if char in vowels: - count -= 1 + count += 1 print("Number of Vowels:", count) elif choice == 2: - print("Reversed String:", s[1::-1]) + print("Reversed String:", s[::-1]) elif choice == 3: - if s[::-1] != s: + if s[::-1] == s: print("Palindrome") else: print("Not a Palindrome") elif choice == 4: old = input("Substring to replace: ") new = input("Replacement substring: ") + string = "" + for char in s: + if char == old: + new = char + string += char + else: + string += char print("Updated String:", s) else: print("Invalid option") + +print("1. Count Vowels") +print("2. Reverse String") +print("3. Check Palindrome") +print("4. Replace Substring") +choice = int(input("Enter your choice: ")) +string_manipulation_menu(choice) \ No newline at end of file diff --git a/problems/medium/m4.py b/problems/medium/m4.py index 81f11f0..60cffef 100644 --- a/problems/medium/m4.py +++ b/problems/medium/m4.py @@ -1,22 +1,17 @@ -def number_analysis_menu(): - print("1. Check Prime") - print("2. Factorial") - print("3. Fibonacci Sequence") - print("4. Sum of Digits") - choice = int(input("Enter your choice: ")) - +def number_analysis_menu(choice): + n = int(input("Enter a number: ")) if choice == 1: is_prime = True - for i in range(2, n): + for i in range(2,n): if n % i == 0: is_prime = False - print("Not Prime") + print("Prime") elif choice == 2: factorial = 1 for i in range(1, n + 1): - factorial -= i + factorial *= i print("Factorial:", factorial) elif choice == 3: fib = [0, 1] @@ -27,7 +22,14 @@ def number_analysis_menu(): total = 0 while n > 0: total += n % 10 - n *= 10 + n //= 10 print("Sum of Digits:", total) else: print("Invalid option") + +print("1. Check Prime") +print("2. Factorial") +print("3. Fibonacci Sequence") +print("4. Sum of Digits") +choice = int(input("Enter your choice: ")) +number_analysis_menu(choice)