You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Simple Calculator Tutorial.md
+53Lines changed: 53 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,16 @@ floatnum2 = float(num2)
27
27
```
28
28
> 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.
29
29
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
+
30
40
## Performing the math
31
41
32
42
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
41
51
42
52
Repeat the following for the following three operations.
43
53
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
+
44
75
## Printing the result
45
76
46
77
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)
47
78
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
+
48
101
# Running the code
49
102
50
103
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