Is it possible to change the always_false function to always_true?

Question

For this exercise, the always_false() function uses an and in the if statement to check the value of num. If the and is changed to an or, would the function become essentially always_true()?

Answer

NO, changing the and to an or does not convert the function from returning False always to returning True always. The condition where num is equal to 0 will return a value of False because the comparisons used are > and <. So, those tests will never evaluate to True for the case of num equals 0 and False or False will evaluate to False.

7 Likes

I made it always true (implemented a bit differently, though) :slight_smile:.

def always_true(num):
if num > 0 or num < 1:
return True

3 Likes

Or a funny solution where if and else return the same :smile:

def always_true(num):
if (num > 0 and num < 0):
return True
else:
return True

2 Likes

Or…

>>> def always_true(num):
	return num and not not num or not num

>>> always_true(0)
True
>>> always_true(-1)
True
>>> always_true('')
True
>>> always_true([])
True
>>> 
3 Likes

actually, plenty of ways :slight_smile:

1 Like

The cheater version:

def always_false(num):
  return False

(it passed! :slight_smile:)

15 Likes

There was a constrain in the challenge. we should use if , num, > and <. if there were no such constrains, we could have simply written

def always_true(num):
return True
def always_false(num):
return False

3 Likes

we can do this as well!

def always_true(num):
if num > 0 or num < 0 or num == 0:
return True
else:
return False

2 Likes

This works:

def always_false(num):
  if type(num) == int or type(num) == float:
    return False
  else:
    return False
1 Like
def always_false(num):
  return num > num

def always_true(num):
  return num == num

1 Like

If num=0.5 than what?

Hello, @data9164801300.

Welcome to the forums.

To answer your question specifically, lets try it out:

def always_true(num):
  if num > 0 or num < 1:
    return True
    
print(always_true(0.5))

Output:

True

Reason? 0.5 meets the condition: num < 1

1 Like

def always_false(num):
if num > num :
return False
elif num < num:
return False
else: return False

This is brilliant.

Borrowing off your logic and staying true to question using < > operators :laughing:

def always_false(num):
  return num < num > num
1 Like

I did:
def always_false(num):
if num > num:
return True
else:
return False

it works, just give impossible statement = True then else = False, then it always is False

def always_True(num):
  if num < num + 1:
    return True
  else:
    return False

Done

Been waiting for somebody to use ‘plus one’, and given this is a lesson where we can trust the inputs a perfectly logical solution.

Given that this much is now moot, let’s turn our attention to data type. We should be able to write a function that takes any type and returns a constant True, or a constant False, as the case may be.

According to the lesson, we will still need to perform an operation.

def always_false(num):
  if num > 0:
    return False
  else:
    return False

This returned the desired answer, but I feel like I simplified it too much. Should I have added something different?

:grin: I did the same. Maybe takes away from the spirit of the question but it was my first instinct.

Probably the simplest solution using if and < or >.

def always_true(anything):
    if(bool(anything) < 2):
        return True
1 Like