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

Python Solutions

Uploaded by

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

Python Solutions

Uploaded by

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

Python Solutions

Question 24d

Shape Output:

**

* *

* *

* *

**

Solution:

n = 4 # number of rows

# Upper half

for i in range(1, n + 1):

x=1

while x < 2 * i:

if x == 1 or x == 2 * i - 1:

print('*', end='')

else:

print(' ', end='')

x += 1

print()

# Lower half

for i in range(n - 1, 0, -1):


x=1

while x < 2 * i:

if x == 1 or x == 2 * i - 1:

print('*', end='')

else:

print(' ', end='')

x += 1

print()

Question 25

def add(x, y):

return x + y

def subtract(x, y):

return x - y

def multiply(x, y):

return x * y

def divide(x, y):

if y == 0:

return "Cannot divide by zero!"

return x / y

print("Select operation:")

print("1.Add")

print("2.Subtract")
print("3.Multiply")

print("4.Divide")

while True:

choice = input("Enter choice (1/2/3/4): ")

if choice in ('1', '2', '3', '4'):

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

if choice == '1':

print(f"{num1} + {num2} = {add(num1, num2)}")

elif choice == '2':

print(f"{num1} - {num2} = {subtract(num1, num2)}")

elif choice == '3':

print(f"{num1} * {num2} = {multiply(num1, num2)}")

elif choice == '4':

print(f"{num1} / {num2} = {divide(num1, num2)}")

else:

print("Invalid Input")

Question 26

def count_vowels_and_consonants(string):

vowels = "aeiouAEIOU"

vowel_count = 0

consonant_count = 0
for char in string:

if char.isalpha(): # Check if character is an alphabet

if char in vowels:

vowel_count += 1

else:

consonant_count += 1

return vowel_count, consonant_count

string = input("Enter a string: ")

vowels, consonants = count_vowels_and_consonants(string)

print(f"Vowels: {vowels}, Consonants: {consonants}")

You might also like