Question

using the following solve using python and use the code at bottom of page to run...

using the following solve using python and use the code at bottom of page to run test MUST PAST ALL TESTS

def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and
false_result otherwise.

>>> if_function(True, 2, 3)
2
>>> if_function(False, 2, 3)
3
>>> if_function(3==2, 3+2, 3-2)
1
>>> if_function(3>2, 3+2, 3-2)
5
"""
if condition:
return true_result
else:
return false_result
def with_if_statement():
"""
>>> with_if_statement()
1
"""
if c():
return t()
else:
return f()

def with_if_function():
eturn if_function(c(), t(), f())

def c():
"*** YOUR CODE HERE ***"

def t():
"*** YOUR CODE HERE ***"

def f():
"*** YOUR CODE HERE ***"

def _test():
import doctest
doctest.testmod(verbose=True)

if __name__ == "__main__":
_test()

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import doctest

def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result

def with_if_statement():
    """
    >>> with_if_statement()
    1
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    return True

def t():
    return 1

def f():
    return 0

def _test():
    doctest.testmod(verbose=True)

if __name__ == "__main__":
    _test()

Please upvote the solution if it helped. Thanks!

Note: Please comment below if you have any doubts

Add a comment
Know the answer?
Add Answer to:
using the following solve using python and use the code at bottom of page to run...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • AND logic gate simulation in Python: Please fix the code below so that it efficiently performs...

    AND logic gate simulation in Python: Please fix the code below so that it efficiently performs the operation of the AND logic gate in Python. (Leave comments in the edited code about what you changed) #if both values are 1, return true, other wise return false print ("Logic Gate Simulation: AND gate") def AND(x, y):     if x == True and y == True:         return True        else:         return False    def main():     x = bool(input("Please...

  • Hi I'm trying to write a code for a web server in python with flask. This...

    Hi I'm trying to write a code for a web server in python with flask. This is what I have so far from flask import Flask app = Flask(__name__) @app.route('/') #first endpoint i.e. "http://localhost/" def index(): return 'hello' #return data in string if __name__ == '__main__': app.run(debug=True) After running the code, I'm given a address to the web with the text hello. The problem is that this only works with my computer that is running the code. If I try...

  • The code: def isPalindrome(text): """ >>> isPalindrome("alula") True...

    The code: def isPalindrome(text): """ >>> isPalindrome("alula") True >>> isPalindrome("love") False >>> isPalindrome("Madam") True >>> isPalindrome(12.5) False >>> isPalindrome(12.21) False >>> isPalindrome("Cigar? Toss it in a can.! It is so tragic.") True >>> isPalindrome("travel.. a town in Alaska") False """ # --- YOU CODE STARTS HERE if type(text) is not str: return False l = len(text) - 1 i = 0 while i < l: if text[i] in string.punctuation or text[i] == ' ': i += 1 elif text[l] in...

  • using python 3: Do not change the tests. Write the body of the distFromOrigin method. class...

    using python 3: Do not change the tests. Write the body of the distFromOrigin method. class Point: def __init__(self, initX, initY): """ Create a new point at the given coordinates """ self.__x = initX self.__y = initY def getX(self): """ Get its x coordinate """ return self.__x def getY(self): """ Get its y coordinate """ return self.__y def __str__(self): """ Return a string representation of self """ return "({}, {})".format(self.__x, self.__y) def distFromOrigin(self): """ Return the distance between self and...

  • Number 1) Which of the following statements imports a module into the default namespace? a. from...

    Number 1) Which of the following statements imports a module into the default namespace? a. from temperature import * b. import temperature as t c. import temperature as temp d. import temperature Number 2) Which of the following statements imports a module into the global namespace? a.from temperature import *   b. import temperature as temp c. import temperature as global d. import temperature Number 3) Code Example 4-2 def get_volume(width, height, length=2):     volume = width * height * length...

  • Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to...

    Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to "Add a counter to report how many searches have been done for each item searched for." Have to follow this: 1) you'll create a counter variable within the function definition, say after "the top = len(myList)-1" line and initialize it to zero. 2) Then within the while loop, say after the "middle = (bottom+top)//2" line, you'll start counting with "counter += 1" and 3)...

  • Submit a single file named hw0.py that contains the solutions to the problems below. When you...

    Submit a single file named hw0.py that contains the solutions to the problems below. When you are finished, test your solutions using the doctest: copy the file hw0TEST.py from d2l into your working folder. include the following code at the bottom of the module hw0.py, and then run your module.   Fix all failures before you submit your solutions. if __name__=='__main__':     import doctest     print( doctest.testfile( 'hw0TEST.py')) Write a function moreOdds that accepts one argument, a list of integers.   The function then...

  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • In Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal...

    In Python, revise this code. define convert(s): """ Takes a hex string as input. Returns decimal equivalent. """ total = 0 for c in s total = total * 16 ascii = ord(c if ord('0) <= ascii <= ord('9'): #It's a decimal number, and return it as decimal: total = total+ascii - ord('0') elif ord('A") <= ascii <= ord('F'): #It's a hex number between 10 and 15, convert and return: total = total + ascii - ord('A') + 10 else...

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT