【问题标题】:Support with Logic Gate Simulation program in Python支持 Python 中的逻辑门仿真程序
【发布时间】:2016-04-26 13:24:53
【问题描述】:

我正在尝试在 python 中创建一个逻辑门模拟程序,以便用户可以选择他们想要模拟的逻辑门的类型。一旦选择,他们就可以输入输入,程序应该将所选逻辑门的输出值返回给用户。

这是我目前所拥有的:

print ("Logic Gate Simulation")

def AND (a,b):

    if a == 1 and b == 1:
        return 1
    else:
        return 0



def NAND (a,b):
    if a == 1 and b == 1:
        return 0
    else:
        return 1


def OR(a,b):
    if a == 1:
        return 1
    elif b == 1:
        return 1
    else:
        return 0


def NOR (a,b):
    if a != b:
        return 1
    else:
        return 1

def Main():
    question = input("what type of gate do you want to simulate - OR, AND  or NAND?  ")

    if question == 'AND':
        a = input("enter value for input 1")
        b = input("enter value for input 2")
        x= AND(a,b)
        print (x)

    else:
        print ("")
Main()

当我运行程序并输入 AND 时,输入 1 和 1 它仍然返回 0,我不明白为什么。

【问题讨论】:

    标签: function boolean logic


    【解决方案1】:

    这行得通:

    def AND (a,b):
        a=int(a)
        b=int(b)
        if a == 1 and b == 1:
            return 1
        else:
            return 0
    

    你必须告诉 python a 和 b 是整数 - (有比显示的方法更好的方法)

    【讨论】:

      【解决方案2】:

      这是我的代码版本(我不得不这样做作为 AS Level 的家庭作业,这可能会对你有所帮助!):

      print("Logic Gate Calculator")
      
      
      def AND(a, b):  # AND Gate
          a = int(a)
          b = int(b)
          if a == 1 and b == 1:  # AND Gate logic
              return 1
          else:
              return 0
      
      
      def NAND(a, b):  # NAND Gate
          a = int(a)
          b = int(b)
          if a == 1 and b == 1:  # NAND Gate logic
              return 0
          elif a == 1 and b == 0:
              return 0
          elif a == 0 and b == 1:
              return 0
          else:
              return 1
      
      
      def OR(a, b):  # OR Gate
          a = int(a)
          b = int(b)
          if a == 1:  # OR Gate Logic
              return 1
          elif b == 1:
              return 1
          else:
              return 0
      
      
      def NOR(a, b):  # NOR Gate
          a = int(a)
          b = int(b)
          if a == 1 and b == 0:  # NOR Gate Logic
              return 1
          elif a == 0 and b == 1:
              return 1
          elif a == 0 and b == 0:
              return 1
          else:
              return 0
      
      
      def XOR(a, b):  # XOR Gate
          a = int(a)
          b = int(b)
          if a == 1 and b == 0:  # XOR Gate Logic
              return 1
          elif a == 1 and b == 1:
              return 1
          else:
              return 0
      
      
      def main():  # The main program
          run = True
          while run:  # While loop
              question = input("What type of gate do you want to use OR, AND, NOR, or NAND or (Q)uit")  # Logic Gate chooser
              if question == "AND" or question == "and" or question == "And":  # If the user selects AND
                  a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
                  b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
                  x = AND(a, b)  # Calling the AND Function
                  print("The output will be:", x)  # Output result
              elif question == "OR" or question == "or" or question == "Or":  # If the user selects OR
                  a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
                  b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
                  x = OR(a, b)  # Calling the OR Function
                  print("The output will be:", x)  # Output result
              elif question == "NOR" or question == "nor" or question == "Nor":  # If the user selects NOR
                  a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
                  b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
                  x = NOR(a, b)  # Calling the NOR function
                  print("The output will be:", x)  # Output result
              elif question == "NAND" or question == "nand" or question == "Nand":  # If the user selects NAND
                  a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
                  b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
                  x = NAND(a, b)  # Calling the NAND function
                  print("The output will be:", x)  # Output result
              elif question == "XOR" or question == "xor" or question == "Xor":  # If the user selects XOR
                  a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
                  b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
                  x = XOR(a, b)  # Calling the XOR function
                  print("The output will be:", x)  # Output result
              elif question == "Q" or question == "q":  # Quiting the program
                  run = False
              else:
                  print("Please enter one of the shown logic gates")  # Error handling
      
      
      main()
      

      【讨论】:

        【解决方案3】:

        a=真 b=真

        输出=a 和 b 打印(输出)

        a=真 b=假

        输出=a 和 b 打印(输出)

        请试试这个

        【讨论】:

        • 输入框正在合并行
        【解决方案4】:

        它有效。采用 如果 a == 1: 如果 b == 1: 返回 1 别的 返回 0

        【讨论】:

          猜你喜欢
          • 2023-03-27
          • 2012-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-20
          • 1970-01-01
          相关资源
          最近更新 更多