Created
August 15, 2019 14:42
-
-
Save elucian/8600240fe89913c5e1a51f2d4661b4e1 to your computer and use it in GitHub Desktop.
Switch simulation using Python functions
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
#define a value holder function | |
# => True | |
def switch(value): | |
switch.value=value | |
return True | |
#define matching case function | |
# => True or False | |
def case(*args): | |
return any((arg == switch.value for arg in args)) | |
# Switch example: | |
print("Describe a number from range:") | |
for n in range(0,10): | |
print(n, end=" ", flush=True) | |
print() | |
# Ask for a number and analyze | |
x=input("n:") | |
n=int(x) | |
while switch(n): | |
if case(0): | |
print ("n is zero;") | |
break | |
if case(1, 4, 9): | |
print ("n is a perfect square;") | |
break | |
if case(2): | |
print ("n is an even number;") | |
if case(2, 3, 5, 7): | |
print ("n is a prime number;") | |
break | |
if case(6, 8): | |
print ("n is an even number;") | |
break | |
print ("Only single-digit numbers are allowed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment