''' switch_case101.py
a switch/case like statement to replace
multiple if/elif/else statements
'''
# make input() work with Python2 and 3
try:
input = raw_input
except NameError:
pass
def switch_case(case):
return "You entered " + {
'1': "one", # if
'2': "two", # elif
'3': "three", # elif
'4': "four" # elif
}.get(case, "a bad number") # else
num = input("Input a number between 1 and 4: ")
print(switch_case(num))
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.