Created
December 3, 2018 16:13
-
-
Save mobileappconsultant/c8824ea0e259ec26b20d52a1436d0b79 to your computer and use it in GitHub Desktop.
Simple calculator task in the Data Science lecture, implemented using a "pseudo" switch statement to replace the multiple if/else statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getpass | |
from getpass import getpass | |
def demo(argument): | |
options = { | |
1: one() , | |
2: two(), | |
3: three(), | |
4: four() | |
} | |
return options.get(argument, "nothing") | |
def one(): | |
result = number_2 + number_1 | |
return ("\n The output of {} + {} is {} ".format(number_1, number_2,result)) | |
def two(): | |
result = number_2 - number_1 | |
return( "\n The output of {} - {} is {} ".format(number_2, number_1, result)) | |
def three(): | |
result = number_2 * number_1 | |
return( "\n The output of {} * {} is {} ".format(number_1, number_2,result)) | |
def four(): | |
result = number_2/number_1 | |
return( "\n The output of {} / {} is {} ".format(number_1, number_2, result)) | |
print("""1. Addition\n 2. Subtraction \n3. Multiplication\n 4. Division\n""") | |
operator = int(input("Choose your operator by entering the corresponding number ")) | |
number_1 = int(input("Enter the first number")) | |
number_2 = int(input("Enter the 2nd number ")) | |
print(demo(operator)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. Will do mine later