In: Computer Science
The primary objective of this assignment is to
reinforce the concepts of string processing.
Part A: Even or Odd [10 marks]
For this first part of the assignment, write a
function that will generate a random number within some range (say,
1 to 50), then prompt the user to answer if the number is even or
odd. The user will then input a response and a message will be
printed indicated whether it was correct or not. This process
should be repeated 5 times, using a new random number for each
play, regardless of whether the user was correct or not.
Notes
A correct response for an even number can be any of
the following: “even”, “Even”, “EVEN”, “e”, “E”. “EiEiO”. The main
criterion is that a correct response begins with the letter “e”,
regardless of case. The same goes for an odd number – a correct
response should begin with the letter “o”.
If the user enters an invalid response, they should be
notified. However, an invalid response still counts as one of the 5
plays.
The solution should contain a loop (for or while) and
a few if statements. An example execution is shown below:
>>> assign2PartA()
Is 41 odd or even? Odd
Correct
Is 48 odd or even? Even
Correct
Is 3 odd or even? e
Incorrect
Is 20 odd or even? o
Incorrect
Is 42 odd or even? xyz
You did not enter a correct reply.
Please answer using Odd or Even
Part B: Vowel Counting [10 marks]
The second task is to write a function that counts,
and prints, the number of occurrences for each vowel (a, e, i, o
and u) in a given phrase and outputs a (new) phrase with the vowels
removed. The phrase should be accepted as a parameter to the
function. If the (original) phrase is empty, you should output an
appropriate error message, and obviously with no vowel counts. If
the phrase contains no vowels, a message should be displayed, and
the count can be omitted – since there is no point in displaying 0
for each vowel! A message should also be displayed if the phrase
contains only vowels, but the counts should still be
displayed.
Notes
Recall that you can use the tab escape character
(“\t”) for spacing to align the text, or use a given width in the
f-string (print(f"{count_a:4}, ……”), where the value of count_a
here is right justified in 4 columns.)
Be sure to correctly handle both upper- and lower-case
letters (e.g. both “a” and “A” should be counted as an instance of
the letter “A”.) The new phrase (with vowels removed) should
preserve the letter cases from the original phrase. Be sure to show
output for all possible scenarios in your submitted
output.
>>> assign2PartB("Remember that context here
defines \"+\" as 'Concatenate'")
A E I O U
4 10 1 2 0
The original phrase is: Remember that context here
defines "+" as 'Concatenate'
The phrase without vowels is: Rmmbr tht cntxt hr dfns "+" s
'Cnctnt'
>>>
assign2PartB("bcdfghjklmnpqrstvwxyz")
The phrase contains no vowels:
bcdfghjklmnpqrstvwxyz
>>> assign2PartB("aeiouAEIOU")
A E I O U
2 2 2 2 2
The phrase contains only vowels: aeiouAEIOU
>>> assign2PartB("")
The input phrase is empty!
>>>
using python
Code:
#importing module random
import random
#function named demo
def demo():
#this loop iterates 5 times
for i in range(0,5):
#generating a random number
n=random.randint(1,50)
#taking input from user
a=input("is "+str(n)+" even or odd:
")
#selection statements based on
given conditions
if(n%2==0):
#checks the
first character of input string
if(a[0]=="E" or
a[0]=="e"):
print("Correct")
elif(a[0]=="O"
or a[0]=="o"):
print("Incorrect")
else:
print("You didnot enter a Correct reply")
else:
if(a[0]=="E" or
a[0]=="e"):
print("Incorrect")
elif(a[0]=="O"
or a[0]=="o"):
print("Correct")
else:
print("You didnot enter a Correct reply")
#function named vowel
def vowelcounter(input1):
#variables storing count of each vowel
a=0
e=0
i=0
o=0
u=0
#length of string input
l=len(input1)
#if string is empty
if(a==""):
print("The input phrase is
empty!")
#else
else:
#checking each character in the
string and
#incrementing corresponding vowel
counter
for j in input1:
if(j=='a' or
j=='A'):
a=a+1
if(j=='e' or
j=='E'):
e=e+1
if(j=='i' or
j=='I'):
i=i+1
if(j=='o' or
j=='O'):
o=o+1
if(j=='u' or
j=='U'):
u=u+1
#sum of all vowel counters
s=a+e+i+o+u
#if sum is equal to 0
if(s==0):
print("The
phrase contains no vowels: "+input1)
#if sum is equal to length
elif(s==l):
#printing count
of each vowel
print("A E I O
U")
print(str(a)+"
"+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
print("The
phrase contains only vowels:"+input1)
#else
else:
#printing count
of each vowel
print("A E I O
U")
print(str(a)+"
"+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
print("The
original phrase is: "+input1)
vowels = ('a',
'e', 'i', 'o', 'u','A','E','I','O','U')
#removing vowels
from input1
for k in
input1:
if(k in vowels):
input1=input1.replace(k,"")
#printing
print("The
phrase without vowels is: "+input1)
#main function
def main():
#caling each function
demo()
vowelcounter("Remember that context here defines \"+\"
as 'Concatenate'")
vowelcounter("bcdfghjklmnpqrstvwxyz")
vowelcounter("aeiouAEIOU")
vowelcounter("")
if __name__ == '__main__':
main()
Output:
Code Screenshot:
code snippet:
#importing module random
import random
#function named demo
def demo():
#this loop iterates 5 times
for i in range(0,5):
#generating a random number
n=random.randint(1,50)
#taking input from user
a=input("is "+str(n)+" even or odd: ")
#selection statements based on given conditions
if(n%2==0):
#checks the first character of input string
if(a[0]=="E" or a[0]=="e"):
print("Correct")
elif(a[0]=="O" or a[0]=="o"):
print("Incorrect")
else:
print("You didnot enter a Correct reply")
else:
if(a[0]=="E" or a[0]=="e"):
print("Incorrect")
elif(a[0]=="O" or a[0]=="o"):
print("Correct")
else:
print("You didnot enter a Correct reply")
#function named vowel
def vowelcounter(input1):
#variables storing count of each vowel
a=0
e=0
i=0
o=0
u=0
#length of string input
l=len(input1)
#if string is empty
if(a==""):
print("The input phrase is empty!")
#else
else:
#checking each character in the string and
#incrementing corresponding vowel counter
for j in input1:
if(j=='a' or j=='A'):
a=a+1
if(j=='e' or j=='E'):
e=e+1
if(j=='i' or j=='I'):
i=i+1
if(j=='o' or j=='O'):
o=o+1
if(j=='u' or j=='U'):
u=u+1
#sum of all vowel counters
s=a+e+i+o+u
#if sum is equal to 0
if(s==0):
print("The phrase contains no vowels: "+input1)
#if sum is equal to length
elif(s==l):
#printing count of each vowel
print("A E I O U")
print(str(a)+" "+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
print("The phrase contains only vowels:"+input1)
#else
else:
#printing count of each vowel
print("A E I O U")
print(str(a)+" "+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
print("The original phrase is: "+input1)
vowels = ('a', 'e', 'i', 'o', 'u','A','E','I','O','U')
#removing vowels from input1
for k in input1:
if(k in vowels):
input1=input1.replace(k,"")
#printing
print("The phrase without vowels is: "+input1)
#main function
def main():
#caling each function
demo()
vowelcounter("Remember that context here defines \"+\" as 'Concatenate'")
vowelcounter("bcdfghjklmnpqrstvwxyz")
vowelcounter("aeiouAEIOU")
vowelcounter("")
if __name__ == '__main__':
main()