Skip to content

Commit dc6a5cd

Browse files
authored
Formatted block code as python
1 parent 37bd4f7 commit dc6a5cd

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Simple Calculator Tutorial.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ In python, user input on the command line can be taken by using the command `inp
1111
## Getting input from the user for a calculation
1212

1313
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")
1516
operation = input("Operation?\n")
1617
num2 = input("Your Second Number?\n")
1718
```
@@ -20,16 +21,18 @@ This code will print a prompt asking for a first number to the screen, ask for i
2021
## Typecasting the inputs
2122

2223
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)
2426
floatnum2 = float(num2)
2527
```
2628
> 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.
2729
2830
## Performing the math
2931

3032
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
3336
```
3437
This checks to see if the user wanted to add the two numbers, and if they did, it adds them together.
3538
> 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
4851

4952
# Done!
5053

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

Comments
 (0)