Skip to content

Instantly share code, notes, and snippets.

@mobileappconsultant
Created December 3, 2018 16:13
Show Gist options
  • Save mobileappconsultant/c8824ea0e259ec26b20d52a1436d0b79 to your computer and use it in GitHub Desktop.
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
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))
@orossy
Copy link

orossy commented Dec 3, 2018

Thanks for sharing. Will do mine later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment