In: Computer Science
Write an example of a Python while loop that is infinite, calls the functions first(), second() and third() in that order in the body of the loop, but skips calling second() and third() if first() returns a value of False, and exits if second() returns a value of False.
Declare a Python function named sumEm() that
accepts three numeric parameters and returns the sum of the three
numbers.
Assume sumEm() is declared in a module named
myMod.py, which your program has imported. Show by
code example how to call sumEm().
Declare a Python function named str2val() that accept a numeric parameter and returns a list containing the numeric value passed, two times the numeric value passed and the string representing the value passed.
What does it mean in Python to say that a function is
“polymorphic”?
What is a function stub in Python, and why are they useful? Show by
code example a stub that uses the pass
keyword.
#--------- infinite.py ------
'''
Write an example of a Python while loop that is infinite,
calls the functions first(), second() and third() in that order
in
the body of the loop, but skips calling second() and third() if
first()
returns a value of False, and exits if second() returns a value of
False.
'''
#import random for returning
#random values of True or False for functions first(),second() and
third()
import random,time
random.seed(time.time())
#python considers 1 and True as same
#as well as 0 and False as same
#functions that returns random 0 or 1 {True or False}
def first():
#generate random integer 0 or 1
return random.randint(0,1)
def second():
return random.randint(0,1)
def third():
return random.randint(0,1)
#infinite while loop
count = 1
while(True):
#call first()
print("Iteration",count)
if(first()):
#if first returns true
#then only call second()
if(second()):
#if second
returns true
#then call
third()
third()
else:
#if not break
from loop
break
count+=1
#===== myMod.py =======
"""
Declare a Python function named sumEm() that accepts three
numeric
parameters and returns the sum of the three numbers.
"""
#sumEm() method that takes 3 numbers
#and returns the sum of them.
def sumEm(one,two,three):
return one + two + three
#-------- sumEm.py -------
#import the sumEm method from myMod.py
from myMod import sumEm
one = 1
two = 2
three = 3
print("Sum of Three Numbers: ")
#call the method using sumEm(one,two,three)
print(one,"+",two,"+",three,"=",sumEm(one,two,three))
#---------- str2Val.py ------------
"""
Declare a Python function named str2val() that accept a numeric
parameter and returns
a list containing the numeric value passed, two times the numeric
value passed and the
string representing the value passed.
"""
def str2val(val):
return [val,2 * val,str(val)]
print("Result of str2Val(10): ",str2val(10))
What does it mean in Python to say that a function is “polymorphic”?
Answer: Polymorphic means having multiple forms.
means we can define methods with same name and differ in length of parameters
ex: sum(a,b)
sum(a)
etc.
Example in python functions is len() method takes a string as argument or array as argument or tuple as argument but the call to it same.
What is a function stub in Python, and why are they useful? Show by code example a stub that uses the pass keyword.
Answer:
Stub in python means declaring class and it's methods without any implementations to them.
it helps in creating interfaces for other classes. just the declaration will be implemented by another child class that are inherited by this class.
#------- stubs.py --------
#class Shape stub.
class Shape:
#just pass.
pass
#------- PLSSSSSSSSSSS LIKE THE ANSWER.AND COMMENT IF YOU HAVE
DOUBTS.