i want to print string(given as input) as number(o/p) and i did it in c using switch case .i want to do the same in python...
wat if python provides switch case??n do we have equivalent of it in python??

Examle:
input:three hundred and fifty(string)
output:350(number)

Recommended Answers

All 2 Replies

In Python a dictionary is used to implement a switch/case statement ...

# a dictionary switch/case like statement to replace
# multiple if/elif/else statements in Python

def switch_case(case):
    return case + " --> " + {
    'one hundred' : '100',
    'two hundred' : '200',
    'three hundred and fifty' : '350'
    }.get(case, "no case available")


# test it
num_str = 'three hundred and fifty'
print(switch_case(num_str))

print(switch_case('one hundred'))

"""
my result -->
three hundred and fifty --> 350
one hundred --> 100
"""

thanx a lot vegaseat...

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.