In: Computer Science
Using Python 3
The primary objective of this assignment is to reinforce the concepts of string processing.
Part A: Even or Odd
For this first part, 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
>>> 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
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
>>> 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!
>>>
** Compiled in Spyder **
CODE:
import random
def assign2PartA():
# Running a for loop for 5 iterations.
for i in range(5):
# Generating a random number between 1 and 50 using random.randint().
num = random.randint(1, 50)
# Creating a question by concatenating the random int and string.
question = 'Is ' + str(num) + ' odd or even? '
# Reading response from the user.
response = input(question)
# Converting the input to upper case, so that we can just check for capital letters.
response = response.upper()
# If the first letter is not 'E' or 'O', then it is an invalid input.
if response[0] != 'E' and response[0] != 'O':
print(' You did not enter a correct reply.\n Please answer using Odd or Even')
# If the random number generated is even and the response starts with 'E',
# then the answer is correct.
elif num % 2 == 0 and response[0] == 'E':
print(' Correct')
# If the random number generated is odd and the response starts with 'O',
# then the answer is correct.
elif num % 2 != 0 and response[0] == 'O':
print(' Correct')
# else the answer is incorrect.
else:
print(' Incorrect')
def assign2PartB(string):
# Initializing a, e, i, o, u as 0.
# To store the number of occurrence.
a = e = i = o = u = 0
# Traversing through the string letter by letter and incrementing the value of the variable
# corresponding to vowels.
for letter in string:
if letter == 'A' or letter == 'a':
a += 1
elif letter == 'E' or letter == 'e':
e += 1
elif letter == 'I' or letter == 'i':
i += 1
elif letter == 'O' or letter == 'o':
o += 1
elif letter == 'U' or letter == 'u':
u += 1
# total -> total number of vowels present in the string.
total = (a + e + i + o + u)
# If the string is empty, print 'phrase is empty'.
if string == "":
print('The input phrase is empty!')
# If there is no vowels, print 'no vowels'.
elif total == 0:
print('The phrase contains no vowels:', string)
else:
# else print the occurrences of each vowels.
print('A \t E \t I \t O \t U')
print(a, '\t', e, '\t', i, '\t', o, '\t', u)
# If all the letters are vowels, print 'only vowels'.
if len(string) == total:
print('The phrase contains only vowels:', string)
# else print the original string and another string wihtout vowels.
else:
print('The original phrase is:', string)
# creating an empty string.
new_string = ""
# Appending all the consonant to new_string.
for letter in string:
if letter not in "aeiouAEIOU":
new_string += letter
print('The phrase without vowels is:', new_string)
# calling assign2PartA()
assign2PartA()
print()
# calling assign2PartB() with different strings
string1 = "Remember that context here defines \"+\" as 'Concatenate'"
string2 = "bcdfghjklmnpqrstvwxyz"
string3 = "aeiouAEIOU"
string4 = ""
assign2PartB(string1)
print()
assign2PartB(string2)
print()
assign2PartB(string3)
print()
assign2PartB(string4)
Screenshots:
refer this to know the proper indentations.
Sample I/O:
Hope this Helps:)
Please consider giving this answer a thumbs up.
If this answer didn't satisfies your requirements or needs modifications, please let me know in the comment section before giving a thumbs down.
Thank you! Stay Safe!!!