repl.it
@eighty/

2-Number Calculator

Python

Performs a math operation on two numbers, with error handling, quick restarting, and color formatting. Now with floats!

fork
loading
Files
  • main.py
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Import Colorama Module
from colorama import Fore, Back, Style

# spacing
print("")

# Alert after import
print(Fore.MAGENTA + "---Script Start---")
print(Style.RESET_ALL, end="")

# spacing
print("")


# Define script
def calc_script():

    # What decimal place to round to?
    print("What decimal place should be rounded to? (10 max)")
    try:
        round_to = int(input())
    except ValueError:
        # error message and restart
        print(Fore.RED + "Error: Invalid integer!")
        print(Fore.CYAN + "---Restarting---")
        print(Style.RESET_ALL, end="")
        print("")
        calc_script()

    if round_to > 10:
        print(Fore.RED + "Error: Decimal limit exceeded!")
        print(Fore.CYAN + "---Restarting---")
        print(Style.RESET_ALL, end="")
        print("")
        calc_script()
    else:
        None

    # spacing
    print("")

    # FIRST NUMBER
    # First number prompt
    print("Enter your first number.")

    # num1 input
    num1_input = input()

    # error handler
    try:
        # num1 check for int
        num1 = int(num1_input)
    except ValueError:
        try:
            # num1 check for float
            num1 = float(num1_input)
        except ValueError:
            # error message and restart
            print(Fore.RED + "Error: Invalid number!")
            print(Fore.CYAN + "---Restarting---")
            print(Style.RESET_ALL, end="")
            calc_script()

    # spacing
    print("")

    # OPERATION
    # Operation prompt
    print("Enter the operation you would like to perform. (+-*/^%)")
    # Operation input
    operation = input()
    # awful if statement / string error handler
    if operation == "+" or operation == "add" or operation == "addition" or operation == "plus" or operation == "-" or operation == "sub" or operation == "subtraction" or operation == "minus" or operation == "subtract" or operation == "*" or operation == "x" or operation == "mult" or operation == "multiply" or operation == "multiplication" or operation == "/" or operation == "div" or operation == "divide" or operation == "division" or operation == "^" or operation == "to the power of" or operation == "exponent" or operation == "power" or operation == "%" or operation == "modulo" or operation == "mod" or operation == "remainder" or operation == "remain":
        None
    else:
        # error message and restart
        print(Fore.RED + "Error: Invalid operator!")
        print(Fore.CYAN + "---Restarting---")
        print(Style.RESET_ALL, end="")
        calc_script()

    # spacing
    print("")

    # SECOND NUMBER
    # Second number prompt
    print("Enter your second number.")

    # num2 input
    num2_input = input()

    # error handler
    try:
        # num2 input
        num2 = int(num2_input)
    except ValueError:
        try:
            num2 = float(num2_input)
        except:
            # error message and restart
            print(Fore.RED + "Error: Invalid number!")
            print(Fore.CYAN + "---Restarting---")
            print(Style.RESET_ALL, end="")
            calc_script()

    # spacing
    print("")

    # Make answer green
    print(Fore.GREEN, end="")

    # CALCULATE ANSWER
    # add
    if operation == "+" or operation == "add" or operation == "addition" or operation == "plus":
        print(num1, "+", num2, "=", round(num1 + num2, round_to))

    # sub
    elif operation == "-" or operation == "sub" or operation == "subtraction" or operation == "minus" or operation == "subtract":
        print(num1, "-", num2, "=", round(num1 - num2, round_to))

    # mult
    elif operation == "*" or operation == "x" or operation == "mult" or operation == "multiply" or operation == "multiplication":
        print(num1, "*", num2, "=", round(num1 * num2, round_to))

    # div
    elif operation == "/" or operation == "div" or operation == "divide" or operation == "division":
        print(num1, "/", num2, "=", round(num1 / num2, round_to))

    # expo
    elif operation == "^" or operation == "to the power of" or operation == "exponent" or operation == "power":
        print(num1, "^", num2, "=", round(num1**num2, round_to))

# modulo
    elif operation == "%" or operation == "modulo" or operation == "mod" or operation == "remainder" or operation == "remain":
        print(num1, "%", num2, "=", round(num1 % num2, round_to))

    # error handler, just in case
    else:
        print(
            Fore.RED +
            "If this message appears, something has gone catastrophically wrong."
        )
        print(Style.RESET_ALL, end="")
        quit()

    # Reset formatting
    print(Style.RESET_ALL, end="")

    # spacing
    print("")

    # RESTART
    # Restart prompt
    print("Would you like to restart? (y/n)")

    # Input
    restart_query = input()

    # Restart on yes
    if restart_query == "yes" or restart_query == "y" or restart_query == "Y":
        print("")
        print(Fore.CYAN + "---Restarting---")
        print("")
        print(Style.RESET_ALL, end="")
        calc_script()

    # Quit on no
    elif restart_query == "no" or restart_query == "n" or restart_query == "N":
        print("")
        print(Fore.YELLOW + "---Stopping---")
        print(Style.RESET_ALL, end="")
        quit()

    # error handler
    else:
        print(Fore.RED + "Error: Invalid action!")
        print(Fore.YELLOW + "---Stopping---")
        print(Style.RESET_ALL, end="")
        quit()


# Start script on run
calc_script()