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
+8-5Lines changed: 8 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,8 @@ In python, user input on the command line can be taken by using the command `inp
11
11
## Getting input from the user for a calculation
12
12
13
13
Querying input from the user for a calculation can be done like so,
14
-
```num1 = input("Hello, What is your First Number?\n")
14
+
```py
15
+
num1 =input("Hello, What is your First Number?\n")
15
16
operation =input("Operation?\n")
16
17
num2 =input("Your Second Number?\n")
17
18
```
@@ -20,16 +21,18 @@ This code will print a prompt asking for a first number to the screen, ask for i
20
21
## Typecasting the inputs
21
22
22
23
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
+
```py
25
+
floatnum1 =float(num1)
24
26
floatnum2 =float(num2)
25
27
```
26
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.
27
29
28
30
## Performing the math
29
31
30
32
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
+
```py
34
+
if operation =="+":
35
+
output=floatnum1+floatnum2
33
36
```
34
37
This checks to see if the user wanted to add the two numbers, and if they did, it adds them together.
35
38
> NOTE: Python is whitespace sensitive, so make sure you indent the second line with two spaces, or the if statement will not compile.
@@ -48,4 +51,4 @@ Running the code is as simple as opening `IDLE`, the official Python editor for
48
51
49
52
# Done!
50
53
51
-
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.
54
+
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