views:

197

answers:

8

if:

x = 0
b = x==0

and i print be it would print 'true'

but if i did:

x = 0
b = x ==3

and i printed be it would be false. In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?

let me explain further:

bool = all(n > 0 for n in list) 

if bool != 'True':
    print 'a value is not greater than zero'

But it prints nothing?

+6  A: 

Something like this you mean?

x = 0
if x != 3:
    print "x does not equal 3"
truppo
While nice, the question morphed; perhaps this is no longer appropriate.
S.Lott
+2  A: 
>>> x = 0
>>> if not x == 3: print 'x does not equal 3'
x does not equal 3

lte me explain further:

>>> list = [-1, 1, 2, 3]
>>> if not all(n > 0 for n in list): print 'a value is not greater than zero'
a value is not greater than zero

# => or shorter ...
>>> if min(list) < 0: print 'a value is not greater than zero'
a value is not greater than zero

note that list is a builtin and shouldn't be used as a variable name.

>>> list
<type 'list'>
>>> list = [1, 2, "value not greater than 0"]
>>> list
[1, 2, "value not greater than 0"]
>>> del list
>>> list
<type 'list'>
...
The MYYN
+4  A: 

An if statement as other answers suggest is a possibility (and you could add an else clause to print something specific in each case). More direct is an if/else operator:

print('equality' if b else 'diversity')

You could also use indexing, since False has the int value 0 and True the int value 1:

print(['different', 'the same'][b])

but I find that a bit less readable than the if variants.

Alex Martelli
+2  A: 
a = lambda b :("not true","true")[b == 3]
print a(3)

will do it for you if you want to put it in a lambda

AutomatedTester
this is the first thing i thought of when the OP further explained the problem.
San Jacinto
If the OP is not sure how to properly use boolean variables, the concept of lambdas might be a little advanced.
Graeme Perrow
It seems to me he understands booleans just fine but was confused between their relationship to strings as well as assignment.
San Jacinto
A: 

You will need to do the printing yourself, as everyone suggested here.

It's worthy to note that some languages (e.g. Scala, Ruby, Groovy) have language features that enable you to write:

x should be(3)

And that will report:

0 should be 3 but is not.

In Groovy, with Spock testing framework, you can write:

def "my test":
  when: x = 0
  expect: x == 3

And that would output:

Condition not satisfied:

x == 3
| |  |
0 |  3
 false

I don't think this possibly cleanly in python though.

notnoop
+3  A: 

Remove the quotes around True:

bool = all(n > 0 for n in list) 

if bool != True:
    print 'a value is not greater than zero'

or, you can also check for False:

bool = all(n > 0 for n in list) 

if bool == False:
    print 'a value is not greater than zero'

There are several other "shortcut" ways of writing it, but since you're a beginner let's not confuse the subject more than necessary.

scwagner
+6  A: 

I think perhaps the following will help alleviate some of your confusion...

>>> 0==0
True
>>> 'True'
'True'
>>> (0==0) == 'True'
False
>>> (0==0) == True
True
>>>
Jorenko
A: 
>>> 'True' is not True
True

'True' is a string

True is a boolean

They have nothing to do with each other, except coincidentally. The string value happens to have the same letters as the boolean literal. But that's just a coincidence.

S.Lott