• API
• FAQ
• Tools
• Archive
daily pastebin goal
37%
SHARE
TWEET

# Untitled

a guest Feb 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. Enter the first number: 2
2. Enter the second number: 10
3. Enter an operator (valid operators are +, -, *, / and pow): pow
4. 1024.0
5.
6. #A basic calculator which supports +, -, *, / and pow
7.
9.     return a + b
10.
11. def sub(a, b):
12.     return a - b
13.
14. def mul(a, b):
15.     return a * b
16.
17. def div(a, b):
18.     return a / b
19.
20. def pow(a, b):
21.     return a ** b
22.
23. if __name__ == "__main__":
24.     while True:
25.         try:
26.             number1 = float(input("Enter the first number: "))
27.             number2 = float(input("Enter the second number: "))
28.         except:
29.             print("That is not a number!")
30.             continue
31.
32.         operator = input("Enter an operator (valid operators are +, -, *, / and pow): ")
33.
34.         #Check if user operator is a valid one
35.         if operator == "+":
37.         elif operator == "-":
38.             func = sub
39.         elif operator == "*":
40.             func = mul
41.         elif operator == "/":
42.             func = div
43.         elif operator == "pow":
44.             func = pow
45.         else:
46.             print("Invalid operator!")
47.             continue
48.
49.         #Print result
50.         print(func(number1, number2))
51.
52. operators = {
54.     "-": sub,
55.     "*": mul,
56.     "/": div,
57.     "pow": pow
58. }
59.
60. func = operators.get(operator, None)
61. if func is None:
62.     print("Invalid operator!")
63.     continue
64.
65. def main():
66.     while True:
67.         number1, number2, operator = get_user_input()
68.         func = operators.get(operator, None)
69.         if func is None:
70.             print("Invalid operator!")
71.             continue
72.
73.         print(func(number1, number2))
74.
75. if __name__ == '__main__':
76.     main()
77.
78. import operator
79.
80. operators = {
82.     "-": operator.sub,
83.     "*": operator.mul,
84.     "/": operator.truediv,
85.     "pow": operator.pow
86. }
87.
88. except ValueError:
89.
91.         return a + b
92.
93.
94.     def sub(a, b):
95.         return a - b
96.
97.
98.     def mul(a, b):
99.         return a * b
100.
101.
102.     def div(a, b):
103.         return a / b
104.
105.
106.     def mypow(a, b):
107.         return a ** b
108.
109.
110.     if __name__ == "__main__":
111.         operator_map = {"+": add, "-": sub, "*": mul, "/": div, "pow": mypow}
112.
113.         while True:
114.             try:
115.                 number1 = float(input("Enter the first number: "))
116.                 number2 = float(input("Enter the second number: "))
117.             except ValueError:
118.                 print("That is not a number!")
119.                 continue
120.
121.             operator = input("Enter an operator (valid operators are +, -, *, / and pow): ")
122.             print(operator_map[operator](number1, number2) if operator in operator_map else "Invalid operator!")
123.
124. def binary_operation(operation, left, right):
125.     """Returns the result of applying operation to its arguments."""
126.     return operator_map[operation](left, right)
127.
128. binary_operation('+', 2, 3)
129. binary_operation(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]))
130.
131. binary_operation(input('operator'), float(input('number')), float(input('number'))
132.
133. binary_operation('*', 5.0 binary_operation('+' 2, 3))
134.
136.     ...
137. if __name__ == '__main__':
138.     while True:
139.         try:
140.             number1 = float(input(...))
141.             number2 = float(input(...))
142.         except ValueError:
143.             print("That is not a number!")
144.             continue
145.         operator = input(...)
148.             print("Invalid operator!")
149.         else:
151.
152. import operator
153.
154. operators = {
156.     '-': operator.sub,
157.     '*': operator.mul,
158.     '/': operator.truediv,
159.     '%': operator.mod,
160.     '^': operator.pow,
161.     '//': operator.floordiv,
162.     '>': operator.gt,
163.     '<': operator.lt,
164.     '=': operator.eq,
165.     '!=': operator.ne,
166.     '>=': operator.ge,
167.     '<=': operator.le,
168. }
169.
171.     try:
172.         return operators[op](num1, num2)
173.     except IndexError:
174.         raise ValueError("Invalid operator")
175.
176. if __name__ == '__main__':
177.     operator_string = ', '.join(operators)
178.     print("Valid operators: {}n".format(operator_string))
179.     while True:
180.         try:
181.             number1 = float(input("Enter the first number: "))
182.             number2 = float(input("Enter the second number: "))
183.         except ValueError:
184.             print("That is not a number!")
185.             continue
186.         except (KeyboardInterrupt, EOFError):
187.             break # exit
188.         op = input("Enter an operator: ")
189.         try: