1) In Python Use a while loop to ask the user to enter a series of alphabets. The loop terminates when the user presses enter. Once the loop terminates, display the number of vowels and consonants entered by the user.
Hint: keep a running count of the number of characters entered by the user. Keep a running count of the number of vowels.
Total number of consonants = total number of characters - total number of vowels.
Assume that the user will only ever enter alphabets.
2)in python Ask the user to enter a number n'. Calculate the sum of all the numbers from 1 to n. Display the average sum of the numbers.
Answer 1:
Program 1:
#Python 3 program:
#take input
alphabet = ''
character =' '
print("Enter alphabet: \n")
#The loop terminates when the user presses enter.
while character != "" : #if user press enter key then null string is captured and our loop terminates
character = input()
alphabet = alphabet+character
vowelstr = [each for each in alphabet if each in "AaEeIiOoUu"]
print("No of Vowels = ",len(vowelstr))
#total number of consonants = total number of characters - total number of vowels.
print("\nNo of Consonants = ", len(alphabet)-len(vowelstr))
Program Code Screenshot:
Program Output Screenshot:
Answer 2:
Program 2:
#take input
n = int(input("Enter the number \'n\'\n"))
sum =0
#this will include numbers from 1 to n as range function generate numbers till n-1 so keeo n+1 below
for i in range(1,n+1):
sum = sum+i
print("Sum : ", sum)
print("\nAverage : ", sum/n)
Program Screenshot:
Output Screenshot:
Get Answers For Free
Most questions answered within 1 hours.