Skip to content

Commit 387c198

Browse files
committed
Added code smippets to simple tutorial
1 parent de0a54b commit 387c198

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Simple Calculator Tutorial.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ floatnum2 = float(num2)
2727
```
2828
> 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.
2929
30+
At this point, the code should look like this:
31+
```py
32+
num1 = input("Hello, What is your First Number?\n")
33+
operation = input("Operation?\n")
34+
num2 = input("Your Second Number?\n")
35+
36+
floatnum1 = float(num1)
37+
floatnum2 = float(num2)
38+
```
39+
3040
## Performing the math
3141

3242
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,
@@ -41,10 +51,53 @@ This checks to see if the user wanted to add the two numbers, and if they did, i
4151
4252
Repeat the following for the following three operations.
4353

54+
55+
At this point, the code should look like this:
56+
```py
57+
num1 = input("Hello, What is your First Number?\n")
58+
operation = input("Operation?\n")
59+
num2 = input("Your Second Number?\n")
60+
61+
floatnum1 = float(num1)
62+
floatnum2 = float(num2)
63+
64+
if operation == "+":
65+
output=floatnum1+floatnum2
66+
if operation == "-":
67+
output=floatnum1-floatnum2
68+
if operation == "*":
69+
output=floatnum1*floatnum2
70+
if operation == "/":
71+
output=floatnum1/floatnum2
72+
73+
```
74+
4475
## Printing the result
4576

4677
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)
4778

79+
At this point, the code should look like this:
80+
```py
81+
num1 = input("Hello, What is your First Number?\n")
82+
operation = input("Operation?\n")
83+
num2 = input("Your Second Number?\n")
84+
85+
floatnum1 = float(num1)
86+
floatnum2 = float(num2)
87+
88+
if operation == "+":
89+
output=floatnum1+floatnum2
90+
if operation == "-":
91+
output=floatnum1-floatnum2
92+
if operation == "*":
93+
output=floatnum1*floatnum2
94+
if operation == "/":
95+
output=floatnum1/floatnum2
96+
97+
print("Your Answer: "+str(output))
98+
```
99+
100+
48101
# Running the code
49102

50103
Running the code is as simple as opening `IDLE`, the official Python editor for Windows, Mac and Linux, opening your code file, and pressing `F5` on your keyboard, or going to the `Run\Run Module` menu item at the top of the window (Windows), or the top of the screen (Mac)

0 commit comments

Comments
 (0)