Is at Python 2.7 and 3.0 something wrong with implementation of bool ? (would concern C too ! )

Here is a snippet written in Python - but error is same as well in 2.7 as in 3.0.
It could affect core-development too - where is the bug (of me or of the hardware ?) :

:slight_smile:

Important to say - no offense ! Here is snippet :

#!/usr/bin/python

import sys
import math
import os

# import bool +++ is not yet there, but in c it is given with <stdbool.h>
# have removed the elder comments - cause WYSIWYG is not working ?
# this snippet is simply intended for to check if circuits are working properly
# it looks weird and trivial, but would help to check if boole in processor is correct ?

def circuit(i):
a = int(sys.argv[1])
b = int(sys.argv[1])
 
        switcher={
                10:'AND', print('Type two binary values ( 0 or 1 each) for AND :')
                print('a={:d}, b={:d}, c2={:d}'.format(f(c2 = (a and b)))
                
                20:'OR', printf('Type two binary values ( 0 or 1 each) for OR :')
                print('a={:d}, b={:d}, c2={:d}'.format(f(c2 = (a or b)))

                30:'NAND', printf('Type two binary values ( 0 or 1 each) for NOT AND :')
                print('a={:d}, b={:d}, c2={:d}'.format(f(c2 = (not a and not b)))
                
                40:'NOR', printf('Type two binary values ( 0 or 1 each) for NOT OR :')
                print('a={:d}, b={:d}, c2={:d}'.format(f(c2 = (not a or not b)))
                
                50:'XOR', printf('Type two binary values ( 0 or 1 each) for XOR :')
                print('a={:d}, b={:d}, c2={:d}'.format(f(c2 = (a xor b)))
                
                60:'BELL', printf('\a')
                
                70:'CLOCK with DATE', from datetime import date
                today = date.today()
                print('Today's date:', today)

         return switcher.get(i,"Invalid operation")
         }
         
         c = print('c2={:d}')
         
         print('Diversity is the freedom for to be different ! (Wisdom of ancient ages)'


Python keywords include ‘and’, ‘or’, ‘not’. Note that they are all in lowercase.

Writing your code in a code block would make it much more readable for who tries to help you. Anyhow: the line c = int c2 is not valid Python syntax and you cannot interleave statements and expressions in the dictionary definition as you do, ie you cannot have the print(...) lines in your code where you placed them. I don’t see how this has anything to do with the implementation of bool nor why you somehow expect import bool to do something meaningful.

Hello Daniele, ty for hint with “code blocks” - I did not know this term
for to edit further. For the rest of your hints I look after next minutes …

bool or boole is here for to check circuits.
It would say then, if bool is implemented correctly by hardware or
by programming language ? It would depend on error output.

Sorry, for delay … I needed a pause. Had a board in front of my head (could not
see the solution) - it is oh, so simple in python ? (I am caught in syntax of C) :slight_smile: :


#!/usr/bin/python

import sys
import math
import os

x = int(input("Enter 1st bool value (only 1 or 0 delivers correct result) : "))
a = int(x)
y = int(input("Enter 2nd bool value (only 1 or 0 delivers correct result) : "))
b = int(y)
z1 = x and y
z2 = x or y
z3 = not (x and y)
z4 = x ^ y
print(" a and b = ", z1)
print(" a or b = ", z2)
print(" a nand b = ", z3)
print(" a xor b = ", z4)

Now I need a kind of switch - case implementation, how would this work then in the end ?

You copy x/y to a/b, but then never use a or b again. I don’t think they’re necessary.

3 of your functions will return integers if fed integers, but the not in the nand will force the answer to a bool (True, False). You might want to force the output from all to be integers or bool to keep the output consistent.

You could use the operator module or lambda functions and put them in a dict, then call the dict with the operation you want.

import operator

def calculate(function, a, b):
    functions = {
            "and": operator.and_,
            "or": operator.or_,
            "xor": operator.xor,
            "nand": lambda x, y: not(x and y),
            }
    return functions[function](a, b)

print(f"0 and 1 is {calculate('and', 0, 1)}")
print(f"1 xor 0 is {calculate('or', 1, 0)}")
print(f"0 nand 0 is {calculate('nand', 0, 0)}")
1 Like

Hello BowlofRed,

ty - but do I need for this to import operator ?

There is else command ‘bool’ - is there no easier way simply to type like - as it was said - that bool is already inside Python 2.7 or Python 3 - it was said that there is no need for to do ‘import bool’ ? :

bool {'z1'}

?

but do I need for this to import operator ?

If you write it like the above, yes. Is that a problem? It gives you explicit functions that do what the operator does. This lets it be callable from the dictionary. You could do the same without the operator module by writing them all as lambdas (like the nand). Then you wouldn’t need operator, but I prefer to use it.

Bool isn’t an operator, it’s a function. It explicitly converts the truth value of an input into an explicit True or False value. You can use it, but what do you want it for?

>>> bool(0)
False
>>> bool([])
False
>>> bool(34)
True

The second listing is running proper without :

import operator

Apparently side-effect of :

import sys
import math
import os

?

command bool is a function - then it checks bit-wise or simply by function ?
With … :

bool (z1)
bool (z2)
bool (z3) # ... and so on

… I would double-check in the end, what I have written in the 2nd listing.
My test showed, that the results are the same as, what this listing gives out, so
2nd listing is not wrong.
The 3rd listing of you is not wrong too - it is very interesting indeed, but would
mean “re-invention of the wheel”, or not ?

The 4th listing is now at state of this - and it works - otherwise tell me, what is wrong ?! :


import sys
import math
import os
import operator


# we repeat in this script several times to switch on and off gates with command 'not'


x = int(input("Enter 1st bool value (only 1 or 0 delivers correct result) : "))
a = int(x)
y = int(input("Enter 2nd bool value (only 1 or 0 delivers correct result) : "))
b = int(y)
z1 = x and y
z2 = x or y
z3 = not (x and y)
z4 = x ^ y
z5 = (not (x and y))
z6 = (not (x or y))
z7 = (not (not (x and y)))
z8 = (not (x ^ y))
z9 = (not (not (x and y)))
z10 = (not (not (not (x or y))))
z11 = (not (not (not (x and y))))
z12 = (not (not (x ^ y)))

print(" a and b = ", z1)
print(" a or b = ", z2)
print(" a nand b = ", z3)
print(" a xor b = ", z4)
print("(not (x and y)) = ", z5)
print("(not (x or y)) = ", z6)
print("(not (not (x and y))) = ", z7)
print("(not (x ^ y)) = ", z8)
print("(not (not (x and y))) = ", z9)
print("(not (not (not (x or y)))) = ", z10)
print("(not (not (not (x and y)))) = ", z11)
print("(not (not (x ^ y))) = ", z12)

bool ('z1')
bool ('z2')
bool ('z3')
bool ('z4')
bool ('z5')
bool ('z6')
bool ('z7')
bool ('z8')
bool ('z9')
bool ('z10')
bool ('z11')
bool ('z12')
print("\n final check with command # bool # : ", z1, z2, z3, z4, z5, z6, z7, z8, z9, z10, z11, z12)

print("\n Done ! ")
print("\n Diversity is the freedom for to be different ! (Wisdom of ancient ages.)\n")

No. Python has built in types and functions. They define what happens for “standard” (i.e. built in) operators. Think +, - etc. You do not import those, they are so ubiquitous that it was deemed good to built these into the interpreter.

The operator module from the standard library is independent of that. It defines functions for the built in operations.

As @BowlOfRed previously showed, this is defined by the type.

Translated: Put zero as a parameter into bool() returns False, an empty list returns False, but a non-zero integer returns True.

Nothing wrong - I would remove the definition of the a and b variables, these are never used. But it is not wrong.

Thx. Now is all clear.