Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:44:41+00:00 2026-05-28T07:44:41+00:00

This is a question for a homework problem that I cant figure out: Question

  • 0

This is a question for a homework problem that I cant figure out:

Question Beginning

Q3. Let’s try to write a function that does the same thing as an if statement:

def if_function(condition, true_result, false_result):

    """Return true_result if condition is a true value, and false_result otherwise."""

    if condition:
        return true_result
    else:
        return false_result

This function actually doesn’t do the same thing as an if statement in all cases. To prove this fact, write functions c, t, and f such that one of these functions returns the number 1, but the other does not:

def with_if_statement():

    if c():
        return t()
    else:
        return f()

def with_if_function():

    return if_function(c(), t(), f())

Question End

Heres what I figured out:

with_if_statement() does not evaluate f() if c() is true, but with_if_function() evaluates all 3 before checking if c() is true or not.

So, I thought of assigning a global variable in c(), and changing its value in f()

heres my code (which does not work):

def c():

    try:
        global x
    except NameError:
        x=1
    if x==1:
        return True
    else:
        return False

def t():

    if x==1:
        return (1)
    else:
        return (0)

def f():

    global x
    x=2
    if x==1:
        return (1)
    else:
        return (0)

can anyone help me figure out the answer? Thanks..!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:44:42+00:00Added an answer on May 28, 2026 at 7:44 am

    The global statement shouldn’t throw a NameError (and so you won’t run x=1 in c()). I would try rewriting your code without using exceptions, they won’t be necessary to solve this and are making it more complicated than it needs to be. Using a global variable and having side effects in your functions is certainly the right track.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve this problem, its not a homework question, its just code
This is a homework problem that I have. I have been doing some research
Disclaimer: This is for a homework assignment, but the question is not regarding the
I need to write a function that can read a file, and add all
I've been thinking about this homework question for a bit now. Given an number
This is not a homework problem. I am only going over a freely available
This IS NOT a Homework question! While building my current student database project I
This is NOT a homework question, actually I am doing this for fun. Here
This is a homework question, I got the basics down, but I can't seem
-- EDIT -- Worth to point out. While having a problem with different homework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.