Professional Documents
Culture Documents
APP (998) - Tutorial11 Assgn
APP (998) - Tutorial11 Assgn
APP (998) - Tutorial11 Assgn
numbers in an array.
Code :-
def sum_even_and_odd_numbers(arr):
sum_even = 0
sum_odd = 0
if num % 2 == 0:
sum_odd += num
try:
except ValueError:
Code :-
count = 0
if num == target:
try:
except ValueError:
palindromic words in the sentence. Use an array to store the palindromic words.
Code :-
def is_palindrome(word):
word = word.replace(" ",
"").lower()return word ==
word[::-1]
def
find_palindromic_words(sentence)
:words = sentence.split()
palindromic_words = []
for word in words:
if is_palindrome(word):
palindromic_words.append(word)
return palindromic_words
if name == " main ":
input_sentence = input("Enter a sentence: ")
palindromic_words = find_palindromic_words(input_sentence)
if palindromic_words:
or word in palindromic_words:
print(word)else:
Code :-
def remove_duplicates(input_list):
unique_dict = {} result = []
try:
result_list = remove_duplicates(input_list)
except ValueError:
matrices as lists of lists (2D arrays) and then multiply them if possible. Make sure to
check if the matrices are compatible for multiplication and handle errors gracefully.
Code :-
123
12345
123
1
Code :-
def print_diamond_pattern(n):
try:
except ValueError as e:
print(f"Error: {e}")
Output :-
8. Write a Python program that simulates a simple guessing game. Generate a random number and have
the user guess it. Provide hints like “too high” or “too low” until they guess correctly.
Code :-
import random
def guessing_game():
while True:
try:
except ValueError:
print("Invalid input. Please enter a valid integer.") if name == " main ":
guessing_game()
Output :-
9. Write a Python program that checks the strength of a password entered by a user. The
program should assess the password based on criteria like length, use of uppercase and
lowercase letters, digits, and special characters. Use control structures and arrays to
Code :-
import string
def check_password_strength(password):
length_criteria = len(password) >= 8
uppercase_criteria = any(char.isupper() for char in password)
lowercase_criteria = any(char.islower() for char in password)
digit_criteria = any(char.isdigit() for char in password)
special_char_criteria = any(char in string.punctuation for char in
password)
is_strong = all([length_criteria, uppercase_criteria, lowercase_criteria,
digit_criteria, special_char_criteria])
evaluation = []
if not length_criteria:
evaluation.append("Password should be at least 8 characters long.")if
not uppercase_criteria:
evaluation.append("Password should contain at least one uppercase
letter.")
if not lowercase_criteria:
evaluation.append("Password should contain at least one lowercase
letter.")
if not digit_criteria:
evaluation.append("Password should contain at least one digit.")
if not special_char_criteria:
evaluation.append("Password should contain at least one special character (e.g.,
!@#$%^&*).")
return is_strong, evaluation if name == " main ":
password = input("Enter your password: ")
is_strong, evaluation = check_password_strength(password) if is_strong:
print("Password is strong!") else:
print("Password is weak. Here are some suggestions:") for criterion in
evaluation:
print("- " + criterion)
Output :-
10. Write a Python program that generates the Fibonacci sequence up to a specified number
Code :-
if n <= 0:
fibonacci_sequence.extend([0, 1])
return fibonacci_sequence a, b = 0, 1
"))
if n < 0:
raise ValueError("Number of terms should be a non-negative
integer.")
except ValueError as e:
print("Error:", e)
Output :-