Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python: Menu-Driven Programs Damian Gordon. Menu-Driven Programs These are programs that display a menu (or list of options), and allows the user to choose.

Similar presentations


Presentation on theme: "Python: Menu-Driven Programs Damian Gordon. Menu-Driven Programs These are programs that display a menu (or list of options), and allows the user to choose."— Presentation transcript:

1 Python: Menu-Driven Programs Damian Gordon

2 Menu-Driven Programs These are programs that display a menu (or list of options), and allows the user to choose what to do. Normally each menu option calls a different module or method within the program. This makes the development of the programs a lot easier, since we can develop each module separately.

3 Simple Calculator

4 Simple Calculator: Modules # MODULE ADD: def add(x, y): return x + y # END ADD.

5 Simple Calculator: Modules # MODULE SUBTRACT: def subtract(x, y): return x - y # END SUBTRACT.

6 Simple Calculator: Modules # MODULE MULTIPLY: def multiply(x, y): return x * y # END MULTIPLY.

7 Simple Calculator: Modules # MODULE DIVIDE: def divide(x, y): return x / y # END DIVIDE.

8 Simple Calculator: Main Program print("Select Operation.") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide")

9 Simple Calculator: Main Program choice = input("Enter choice:") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: "))

10 Simple Calculator: Main Program if choice == '1': print(num1,"+",num2,"=", add(num1,num2)) elif choice == '2': print(num1,"-",num2,"=", subtract(num1,num2)) elif choice == '3': print(num1,"*",num2,"=", multiply(num1,num2)) elif choice == '4': print(num1,"/",num2,"=", divide(num1,num2)) else: print("Invalid input") # ENDIF

11 J-K Flip Flops

12 Q Q J K CLK J-K Flip Flop

13 Q Q J K CLK

14 JK Flip Flops: Modules def NAND(a,b): if a == True and b == True: return False else: return True # END NAND.

15 JK Flip Flops: Modules # FALSE, FALSE Input def FF(): print("Assuming an input of FALSE(J) and FALSE(k)") print("") print(" K J ") print(" | | ") print("+---------------------------------+") print(" [ ] | | [ ]") print(" | | | |") print(" False False False False") print(" | | | |") print(" ----------- -----------") print(" | | | |") print(" \ / \ /") print(" ------- -------") print(" O O") print(" | |") print(" ",NAND(False,False), " ",NAND(False,False))

16 JK Flip Flops: Modules print(" | |") print(" | [ ] [ ] |") print(" | | | |") print(" | False False |") print(" | | | |") print(" ----------- -----------") print(" | | | |") print(" \ / \ /") print(" ------- -------") print(" O O") print(" | |") print(" ",NAND(True,False), " ",NAND(False,True)) print(" | |") print("+---------------------------------+") print(" | |") print(" !Q Q")

17 JK Flip Flops: Main Program print("Select Inputs to Flip Flop:") print("") print("1. FALSE(j), FALSE(k)") print("2. FALSE(j), TRUE (k)") print("3. TRUE (j), FALSE(k)") print("4. TRUE (j), TRUE (k)") print("")

18 JK Flip Flops: Main Program choice = input("Enter choice(1/2/3/4):") if choice == '1': FF() elif choice == '2': FT() elif choice == '3': print("Function yet to be implemented") elif choice == '4': print("Function yet to be implemented") else: print("Invalid input") # ENDIF

19 etc.


Download ppt "Python: Menu-Driven Programs Damian Gordon. Menu-Driven Programs These are programs that display a menu (or list of options), and allows the user to choose."

Similar presentations


Ads by Google