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...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
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...
1. Write Python program that use a while loop to determine how long it takes for...
1. Write Python program that use a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: The amount of initial investment can be any positive value, you can use $1. 2. Write a function def DoubleInvest(initialInvest) to perform the same functionalities as question1. Make sure to include you...
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...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every...
PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every 6th number down to 300 # (i.e., prints 500, 494, 488, . . . etc., but does not print any number lower than 300). # Write one while-loop that starts at 80 and prints every 12h number thereafter, # but does not print any number greater than 210 # Write one while-loop that prints all the numbers from 30 through 70, # except for...
python Write a Loop class. The contents of a loop object are represented internally as a...
python Write a Loop class. The contents of a loop object are represented internally as a simple python list with the following additional instance variables: loop - a python list containing the elements of the loop list head - which stores the index of the first element of the loop list current - which stores the index of the last element of the loop list size - which stores how many actual elements are in the loop max - which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT