Guest User

Untitled

a guest
Aug 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #Sean, 04/11/2018, To create a calculator to handle binary numbers, addition & subtraction
  2.  
  3.  
  4. def binaryCalculator():
  5. """Binary Calculator that uses a simple menu format for execution
  6. of funtions that convert binary to decimal and decimal to binary. Runs until the user input is 5."""
  7. global first_number
  8. global second_number
  9. global user_input
  10. print("What do you want to do?")
  11. print("1. Enter the binary number")
  12. print("2. Enter the second binary number")
  13. print("3. Add the two binary numbers together")
  14. print("4. Subtract the second binary number from the first")
  15. print("5. Exit the program\n")
  16. user_input = input('Enter Command\n')
  17. first_number_count = 0
  18. first_number_count2 = 0
  19. while user_input != '5':
  20. if user_input == '1':
  21. first_number = (input("Enter the first number:\n"))
  22. print("Your First Number is:", first_number, "in decimal:", bintodec(first_number))
  23. binaryCalculator()
  24. break
  25. if user_input == '2':
  26. second_number = (input("Enter the second number:\n"))
  27. print("Your Second Number is:", second_number, "in decimal:", bintodec(second_number))
  28. binaryCalculator()
  29. break
  30. if user_input == '3':
  31. if "first_number" and "second_number" not in globals():
  32. print("Please Do Commands 1&2 First")
  33. first_number = input("Enter the first number:\n")
  34. second_number = input("Enter the second number:\n")
  35. binaryCalculator()
  36. break
  37. else:
  38. first_number_count += bintodec(first_number)+bintodec(second_number)
  39. print(first_number,"+",second_number,"Sum Binary=",
  40. dectobin(first_number_count),"Sum Decimal=",first_number_count)
  41. binaryCalculator()
  42. break
  43. if user_input == '4':
  44. if "first_number" and "second_number" not in globals():
  45. print("Please Do Commands 1&2 First")
  46. first_number = int(input("Enter the first number:\n"))
  47. second_number = int(input("Enter the second number:\n"))
  48. binaryCalculator()
  49. break
  50. if second_number < first_number:
  51. print("Second Number Should be Larger Than First")
  52. else:
  53. first_number_count2 += int(second_number)-int(first_number)
  54. print(second_number,"-",first_number,"Difference Binary=",first_number_count2,"Difference Decimal="
  55. ,bintodec(first_number_count2))
  56. binaryCalculator()
  57. break
  58. if user_input not in str("1,5"):
  59. print("Invalid Input, Try Again")
  60. binaryCalculator()
  61. break
  62.  
  63.  
  64. def dectobin(n):
  65. """Translate a positive integer from decimal to binary.
  66. Params: n (int): decimal number > 0
  67. Returns: (string) binary number as a int
  68. """
  69. if n == 0:
  70. return ''
  71. else:
  72. return dectobin(n // 2) + str(n % 2)
  73.  
  74.  
  75. def bintodec(x):
  76. """Converts binary into decimal.
  77. Params: x (str): binary number as an int
  78. Returns: (int) decimal number
  79. """
  80. n = 0
  81. for i in str(x):
  82. if i > '1':
  83. print("Please Enter a Binary Number\n")
  84. binaryCalculator()
  85. quit()
  86. n *= 2
  87. if i == '1':
  88. n += 1
  89. return n
  90.  
  91. print(625 % 2)
  92. binaryCalculator()
Add Comment
Please, Sign In to add comment