Question

In: Computer Science

Write an example of a Python while loop that is infinite, calls the functions first(), second()...

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.

Solutions

Expert Solution

#--------- 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.


Related Solutions

Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
write an operational semantic for a while loop in c++
write an operational semantic for a while loop in c++
Write a denotational semantics for do-while loop
Write a denotational semantics for do-while loop
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT