Question

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 enter a 0 or 1: "))
    y = bool(input("Please enter another 0 or 1: "))
    print(AND(x, y))
    return

if __name__ == "__main__":
    main()

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

Please find my modified code.

We only check for second parametr if and only if first parameter is true.

#if both values are 1, return true, other wise return false
print ("Logic Gate Simulation: AND gate")
## AND function that perform AND logic
def AND(a,b):
if a == True : ## if a is true, then check for b
if b == True :
return True
else :
return False
  
## main function defination
def main():
   ## reading first input
x = bool(input("Please enter a 0 or 1: "))
## reading second input
y = bool(input("Please enter another 0 or 1: "))
## calling AND function and printing resilt
print(AND(x, y))
return

## calling main function
if __name__ == "__main__":
main()

→ secondweek secondweek python andgate.py Logic Gate Simulation: AND gate Please enter a 0 or 1: 1 Please enter another0 or 1

Add a comment
Know the answer?
Add Answer to:
AND logic gate simulation in Python: Please fix the code below so that it efficiently performs...
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
  • 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...

  • Write a python code that takes in an number 0-9 and prints out the word of...

    Write a python code that takes in an number 0-9 and prints out the word of the number. For example 1 would print out one. Below is the skeleton of the code that needs to be filled in. def num2string(num): """ Takes as input a number, num, and returns the corresponding name as a string. Examples: num2string(0) returns "zero", num2string(1)returns "one" Assumes that input is an integer ranging from 0 to 9 """ numString = "" ################################### ### FILL IN...

  • while trying to run this in python, i keep getting the error TypeError: int() can't convert...

    while trying to run this in python, i keep getting the error TypeError: int() can't convert non-string with explicit base. any way to fix this? def binaryToDecimal(n): return int(n,2) if __name__ == '__main__': num3 = int(input("Enter value to be converted: ")) print(binaryToDecimal(num3))

  • How do I make my code print out the text in the ServiceNotifier class? Python Code:...

    How do I make my code print out the text in the ServiceNotifier class? Python Code: class ServiceNotifier: #Subject responsible for notifying registered observer objects    Observer = [Email, SMS, Phone]       def attach(self):    #add new observer        Observer.append(insertnewobserverhere)    def dettach(self):    #remove an observer        Observer.pop(insertnewobserverhere)    def servicenotifier(self):        for observers in Observer:            print("Due to the forecast for tomorrow, all university and campus operations will be closed.")       ...

  • 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...

  • Show the source code if possible or code writen with output please and thank you <3...

    Show the source code if possible or code writen with output please and thank you <3 6. Is the following a legal Python program? def proc(x, y): return 2*x + y*y def main(): print(proc(5)) main() 7. Is the following a legal Python program? def proc(x): return 2*x def main(): print(proc(5, 4)) main() 8. Is the following a legal Python program? def proc(x): print(2*x*x) def main(): proc(5) main()

  • 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():...

  • Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from...

    Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...

  • for this code fix the smtax error this is a python code for some reason my...

    for this code fix the smtax error this is a python code for some reason my code isnt liking my returns and it bring up other symtax error so fix the following code answer should pop out the following when runned 1/ 2 + 1/ 3 + 1/ 12 = 11/12 z=' ' def math(x,y):     global z     if y==0 or x==0:         return     if y%x ==0:         z=("1/"+str(int(y/x))         return      if x%y ==0         z+=(int(x/y))    ...

  • Please use Python3. Thanks Find and fix the errors (syntax or logic) in the following code...

    Please use Python3. Thanks Find and fix the errors (syntax or logic) in the following code snippet: # this function creates a 2D list using the height, width, and symbol. It does # not contain any shallow copies of rows def createList(height, width, symbol): board = [] for i in range(len(height)): row = [] for j in range(width): row.append(symbol) board.append(row) def main(): # creates 5 rows of 5 @s myList = createList(5,"@") print(board) main()

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