Skip to content

Commit dd52999

Browse files
committed
Added content to readme(s) and revamped code
1 parent 2be60cf commit dd52999

File tree

5 files changed

+202
-174
lines changed

5 files changed

+202
-174
lines changed

Advanced Calculator Tutorial.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Advanced Calculator
2+
3+
## Goals
4+
5+
This calculator is more advanced in that it includes some operations that require only one numberical input, like log and square root, as well as recalling constants, like pi and such that do not require any numerical input whatsoever. The program we build will have to deal with this, without crashing or bein vumbersome to use.
6+
7+
The aim is to get this calculator to support the following functions: `+,-,*,/,/-,^,!,abs,sin,cos,tan,asin,acos,atan,log10,log,rand,randint,pi,e,tau,M+,M-,MR,degrees->radians,radians->degrees`
8+
9+
The simple calculator program ended once the operation was completed, something we also want to avoid with this calculator.
10+
11+
Since it has a long list of functions, we also want to be able to lit out all of the things the calculator can do.
12+
13+
##

Advanced Calculator.py

Lines changed: 119 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@
33
import random
44
# Define Variables
55
output = 0
6-
process1 = ""
7-
processO = ""
8-
process2 = ""
6+
num1 = ""
7+
operator = ""
8+
num2 = ""
99
memStore = "EMPTY"
10-
# Define Vars To Allow Program To Pass The Input Stage
11-
operationD = False
12-
process1D = False
13-
process2D = False
14-
need2 = True
15-
skipOp = False
1610

1711
# Define Function Listing Function
1812
def abilitiesList():
@@ -44,162 +38,135 @@ def abilitiesList():
4438
print("randint...Returns A Random Number Between The Two Inputs")
4539
print("//////////////////////////////////////////////////////////////////////////")
4640

41+
def askForInput(textPrompt):
42+
num = input(textPrompt)
43+
try:
44+
# Try to typecast the input to a float
45+
float(num)
46+
except ValueError:
47+
# Catch the exception if it is not a number
48+
print("ERROR: Syn: Invalid Num")
49+
else:
50+
# Else, move on
51+
# Typecasting
52+
return float(num)
53+
4754
# While Loop
48-
while(1):
55+
while(True):
4956
# Reset Variables
50-
operationD = False
51-
process1D = False
52-
process2D = False
5357
print("//////////////////////////////////////////////////////////////////////////")
5458
print("Type 'help' for a list of abilities")
55-
# Operation Input
56-
while(operationD == False):
57-
#print("Hello, What is your Operation (Operator)?")
58-
processO = input("Hello, What is your Operation (Operator)? ")
59-
# Is processO == to any of out constants or predefines?
60-
if processO == "help":
59+
# Loop for getting operation
60+
while True:
61+
operator = input("What operation do you want to perform? ")
62+
# Is operator == to any of out constants or predefines?
63+
if operator == "help":
6164
abilitiesList()
62-
processO = ""
63-
skipOp = True
64-
elif processO == "pi":
65+
operator = ""
66+
elif operator == "pi":
6567
print(math.pi)
66-
processO = ""
67-
skipOp = True
68-
elif processO == "e":
68+
operator = ""
69+
elif operator == "e":
6970
print(math.e)
70-
processO = ""
71-
skipOp = True
72-
elif processO == "tau":
71+
operator = ""
72+
elif operator == "tau":
7373
print(math.pi*2)
74-
processO = ""
75-
skipOp = True
76-
elif processO == "MR":
74+
operator = ""
75+
elif operator == "MR":
7776
print(str(memStore))
78-
processO = ""
79-
skipOp = True
80-
elif processO == "M-":
77+
operator = ""
78+
elif operator == "M-":
8179
memStore = "Empty"
8280
print("Memory Cleared")
83-
processO = ""
84-
skipOp = True
85-
elif processO == "M+":
86-
process2 = "0"
87-
operationD = True
88-
skipOp = True
89-
if processO=="rand":
81+
operator = ""
82+
elif operator == "M+":
83+
num2 = "0"
84+
elif operator == "rand":
9085
print(random.random())
91-
skipOp = True
92-
# Have We Given process1 a Valid Operator?
93-
elif processO == "+" or processO== "-" or processO== "*" or processO== "/" or processO== "^" or processO=="/-" or processO=="!" or processO=="Abs" or processO=="d/r" or processO=="r/d" or processO=="M+" or processO=="M-" or processO=="MR" or processO=="sin" or processO=="cos" or processO=="tan" or processO=="asin" or processO=="acos" or processO=="atan" or processO=="log10" or processO=="log" or processO=="randint":
94-
operationD = True
95-
elif skipOp == True:
96-
processO = ""
86+
# Has the user entered in a valid operator?
87+
elif operator == "+" or operator == "-" or operator == "*" or operator == "/" or operator == "^" or operator == "/-" or operator == "!" or operator == "Abs" or operator == "d/r" or operator == "r/d" or operator == "M+" or operator == "M-" or operator == "MR" or operator == "sin" or operator == "cos" or operator == "tan" or operator == "asin" or operator == "acos" or operator == "atan" or operator == "log10" or operator == "log" or operator == "randint":
88+
break
9789
else:
9890
print("ERROR: Invalid Operator")
99-
# Do We Need A 2nd Input?
100-
if processO=="/-" or processO=="!" or processO=="Abs" or processO=="d/r" or processO=="r/d" or processO=="M+" or processO=="sin" or processO=="cos" or processO=="tan" or processO=="asin" or processO=="acos" or processO=="atan" or processO=="log10":
101-
need2 = False
102-
else:
103-
need2 = True
104-
# 1st Number Input
105-
while(process1D == False):
106-
#print("First Number?")
107-
process1 = input("First Number? ")
108-
try:
109-
float(process1)
110-
except ValueError:
111-
print("ERROR: Syn: Invalid Num")
112-
else:
113-
if processO == "asin" or processO == "acos":
114-
if int(process1) > 1 or int(process1) < -1:
115-
print("ERROR: Math: 'asin' and 'acos' commands only accept inputs in range -1 to +1")
116-
process1D = False
117-
else:
118-
process1D = True
119-
else:
120-
process1D = True
121-
# 2nd Number Input
122-
while(process2D == False):
123-
if need2 == False:
124-
process2 = "0"
125-
process2D = True
91+
92+
# Loop for 1st number input
93+
while True:
94+
num1 = askForInput("First Number? ")
95+
# Catch asin and acos out of bounds error case
96+
if (operator == "asin" or operator == "acos") and (float(num1) > 1 or float(num1) < -1):
97+
print("ERROR: Math: 'asin' and 'acos' commands only accept inputs in range -1 to +1")
12698
else:
127-
#print("Second Number?")
128-
process2 = input("Second Number? ")
129-
try:
130-
float(process2)
131-
except ValueError:
132-
print("ERROR: Syn: Invalid Num")
99+
break
100+
101+
# Does the operation require a 2nd input?
102+
if not (operator=="/-" or operator=="!" or operator=="Abs" or operator=="d/r" or operator=="r/d" or operator=="M+" or operator=="sin" or operator=="cos" or operator=="tan" or operator=="asin" or operator=="acos" or operator=="atan" or operator=="log10"):
103+
# Loop for 2nd number input
104+
while True:
105+
num2 = askForInput("Second Number? ")
106+
# Catch x/0 error case
107+
if operator == "/" and num2 == "0":
108+
print("ERROR: Math: Canot divide by 0!")
133109
else:
134-
process2D = True
135-
# x/0 Error
136-
if processO == "/" and process2 == "0":
137-
#Error
138-
print("ERROR: Math: Canot divide by 0!")
139-
else:
140-
# Typecasting
141-
intProcess1 = float(process1)
142-
# Check if to Typecast intProcess2
143-
if need2 == True:
144-
intProcess2 = float(process2)
145-
# Strategic Processing
146-
if processO == "+":
147-
output=intProcess1+intProcess2
148-
print("Your Answer: "+str(output))
149-
if processO == "-":
150-
output=intProcess1-intProcess2
151-
print("Your Answer: "+str(output))
152-
if processO == "*":
153-
output=intProcess1*intProcess2
154-
print("Your Answer: "+str(output))
155-
if processO == "/":
156-
output=intProcess1/intProcess2
157-
print("Your Answer: "+str(output))
158-
if processO == "^":
159-
output=math.pow(intProcess1,intProcess2)
160-
print("Your Answer: "+str(output))
161-
if processO == "/-":
162-
output=math.sqrt(intProcess1)
163-
print("Your Answer: "+str(output))
164-
if processO == "!":
165-
output=math.factorial(intProcess1)
166-
print("Your Answer: "+str(output))
167-
if processO == "Abs":
168-
output=math.fabs(intProcess1)
169-
print("Your Answer: "+str(output))
170-
if processO=="d/r":
171-
output=math.radians(intProcess1)
172-
print("Your Answer: "+str(output))
173-
if processO=="r/d":
174-
output=math.degrees(intProcess1)
175-
print("Your Answer: "+str(output))
176-
if processO=="M+":
177-
memStore = process1
178-
print("Number Stored")
179-
if processO=="sin":
180-
output=math.sin(intProcess1)
181-
print("Your Answer: "+str(output))
182-
if processO=="cos":
183-
output=math.cos(intProcess1)
184-
print("Your Answer: "+str(output))
185-
if processO=="tan":
186-
output=math.tan(intProcess1)
187-
print("Your Answer: "+str(output))
188-
if processO=="asin":
189-
output=math.asin(intProcess1)
190-
print("Your Answer: "+str(output))
191-
if processO=="acos":
192-
output=math.acos(intProcess1)
193-
print("Your Answer: "+str(output))
194-
if processO=="atan":
195-
output=math.atan(intProcess1)
196-
print("Your Answer: "+str(output))
197-
if processO=="log10":
198-
output=math.log10(intProcess1)
199-
print("Your Answer: "+str(output))
200-
if processO=="log":
201-
output=math.log(intProcess2, intProcess1)
202-
print("Your Answer: "+str(output))
203-
if processO=="randint":
204-
output=random.randint(intProcess1, intProcess2)
205-
print("Your Answer: "+str(output))
110+
break
111+
112+
# Calculations
113+
if operator == "+":
114+
output = num1 + num2
115+
print("Your Answer: "+str(output))
116+
if operator == "-":
117+
output = num1 - num2
118+
print("Your Answer: "+str(output))
119+
if operator == "*":
120+
output = num1 * num2
121+
print("Your Answer: "+str(output))
122+
if operator == "/":
123+
output = num1 / num2
124+
print("Your Answer: "+str(output))
125+
if operator == "^":
126+
output = math.pow(num1,num2)
127+
print("Your Answer: "+str(output))
128+
if operator == "/-":
129+
output = math.sqrt(num1)
130+
print("Your Answer: "+str(output))
131+
if operator == "!":
132+
output = math.factorial(num1)
133+
print("Your Answer: "+str(output))
134+
if operator == "Abs":
135+
output = math.fabs(num1)
136+
print("Your Answer: "+str(output))
137+
if operator == "d/r":
138+
output = math.radians(num1)
139+
print("Your Answer: "+str(output))
140+
if operator == "r/d":
141+
output = math.degrees(num1)
142+
print("Your Answer: "+str(output))
143+
if operator == "M+":
144+
memStore = num1
145+
print("Number Stored")
146+
if operator == "sin":
147+
output = math.sin(num1)
148+
print("Your Answer: "+str(output))
149+
if operator == "cos":
150+
output = math.cos(num1)
151+
print("Your Answer: "+str(output))
152+
if operator == "tan":
153+
output = math.tan(num1)
154+
print("Your Answer: "+str(output))
155+
if operator == "asin":
156+
output = math.asin(num1)
157+
print("Your Answer: "+str(output))
158+
if operator == "acos":
159+
output = math.acos(num1)
160+
print("Your Answer: "+str(output))
161+
if operator == "atan":
162+
output = math.atan(num1)
163+
print("Your Answer: "+str(output))
164+
if operator == "log10":
165+
output = math.log10(num1)
166+
print("Your Answer: "+str(output))
167+
if operator == "log":
168+
output = math.log(num2, num1)
169+
print("Your Answer: "+str(output))
170+
if operator == "randint":
171+
output = random.randint(num1, num2)
172+
print("Your Answer: "+str(output))

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Python-Tutorial-Calculator
22
Python Tutorial: Making a simple command line calculator
3+
Assumes the user does not know anything about programming in python.
34

45
# Simple Calculator
56

6-
* Supports the functions '+,-,*,/'
7+
* Supports the functions `+,-,*,/`
8+
9+
Tutorial:
710

811
# Advanced Calculator
912

1013
* Supports the functions `+,-,*,/,^,sqrt,!,abs,sin,cos,tan,arcsin,arccos,arctan,log,log10,rand,randint`
1114
* Also has memory ability `M+,MR,M-`
1215
* Can recall constants `pi,e,tau`
13-
* Can convert `Degrees to Radians, Radians to Degrees`
16+
* Can convert `Degrees to Radians, Radians to Degrees`
17+
18+
Tutorial:

Simple Calculator Tutorial.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Simple Calculator
2+
3+
## Printing to the screen
4+
5+
In python, printing to the command line can be done using the `print()` statement. The only required argument is a string to be printed. For example, in Python 3, `print("Hello World")` will print `Hello World` to the command line.
6+
7+
## Intro to Input
8+
9+
In python, user input on the command line can be taken by using the command `input()`. Putting in a string (optional) as a paramter will give the user a prompt after which they can input text. This statement returns a string with the text the user typed, so it needs to be assigneed to a variable. This can be done in python like so, `myvar = input("Feed me data! ")`. Python is not a language with strongly wyped variables, which means that you do not have to define a type when you create a variable. The interpreter will automatically assign the variable a type the first time it is instantiated with data.
10+
11+
## Getting input from the user for a calculation
12+
13+
Querying input from the user for a calculation can be done like so,
14+
```num1 = input("Hello, What is your First Number?\n")
15+
operation = input("Operation?\n")
16+
num2 = input("Your Second Number?\n")
17+
```
18+
This code will print a prompt asking for a first number to the screen, ask for input, print a prompt asking for an operation to the screen, ask for input, etc. The `\n` at the end of the strings is an escape character that tells python to add a newline to the end of the prompt. I did this so that the user would start typing on the line below the prompt. (personal asthetic taste)
19+
20+
## Typecasting the inputs
21+
22+
In order for us to do math on the numbers that the user typed, we need to convert them to numerical values, as you cannot do math on strings, for obvious reasons. (After all, what is "abc" / "def" anyways?) The method for doing so is called Typecasting. and in Python, you can convert to float (decimal numbers less than 7 digits) by using the float() statement. This can be done for our purposes like so,
23+
```floatnum1 = float(num1)
24+
floatnum2 = float(num2)
25+
```
26+
> NOTE: We are not sanitizing our inputs, and entering in a non-numerical value here will break the code, and sanitizing inputs is really important, but a topic for a later time. For now, we will just hope that no one comes across this code with malicious intentions.
27+
28+
## Performing the math
29+
30+
Now that we have the numbers converted to float types, we are ready to do the math. For simplicity's sake in this lesson, we will use `if()` statements to tell what the user wants to do. In this program, we will support four operations, +,-,* and /, so we will need to have 4 if statements. One if statement for this program will look like,
31+
```if operation == "+":
32+
output=floatnum1+floatnum2
33+
```
34+
This checks to see if the user wanted to add the two numbers, and if they did, it adds them together.
35+
> NOTE: Python is whitespace sensitive, so make sure you indent the second line with two spaces, or the if statement will not compile.
36+
37+
> NOTE: This code also does not sanitize the input for the operation, and if the user did not enter +,-,*, or /, the program will break at the next step, when we print the answer to the screen. Again, for the same reason, we will just hope that no one comes across this code with malicious intentions.
38+
39+
Repeat the following for the following three operations.
40+
41+
## Printing the result
42+
43+
Using the `print()` statement, we can print the result out to the screen. In Python, strings can be concatenated by "adding" them together, as if they were numbers. The code for this step looks like this: `print("Your Answer: "+str(output))`. This code prints the text "Your answer: " concatenated with the output, after it has been typecasted to a string. (You can't concatenate it while it is still formatted as a float)
44+
45+
# Done!
46+
47+
Amazing! You have made a simple command line based calculator! If you understood all of the code in this lesson, and you have gotten the code to compile, please continue onto the advanced tutorial.

0 commit comments

Comments
 (0)