Write a program that has 3 functions: the main function; a function, called getInput(), to
validate the input data; and a function, called findSum(t), to find the summation of even
or odd numbers in a range(0, 10). In particular,
The getInput() function:
This function should be called in the main function to validate the data. It asks the
user to input either “e” (even numbers) OR “o” (old numbers). It keeps
asking the user for valid input unit valid input is given, then it returns the valid input
to the main function.
The findSum(t) function:
t: a "str" which shows whether you want to sum of all the even or odd numbers in the
range(0, 10)
If the value of the argument t is 'e', the function should return the sum of all
even natural numbers in the range(0, 10).
If t has the value 'o', the function should return the sum of all odd natural
numbers in the range(0, 10).
The main function (): controls the flow of the program and prints the result
def getInput():
numberType=""
while numberType!="e" and numberType!="o":
numberType=input("Input either 'e' (even numbers) OR 'o' (odd numbers): ")
return numberType
def findSum(t):
result=0
for i in range(0,10):
if t=="e":
if i%2==0:
result+= i
if t=="o":
if i%2==1:
result+= i
return result
def main():
numberType=getInput()
result=findSum(numberType)
if numberType=="e":
print(f"The sum of all the even numbers in the range(0, 10): {result}")
if numberType=="o":
print(f"The sum of all the odd numbers in the range(0, 10): {result}")
main()
Comments
Leave a comment