-2

I am learning basic python. I use the basics of python for my java homework and built a menu with 3 options. 1st option is simple equation. 2nd option is quadratic equation. 3rd option is calculate electricity bill

print("1. simple equation")
print("2.  quadratic equation")
print("3. calculate electricity bill")
print("Choose")
x=int(input());
if(x>3):
    print("no update")
elif(x==1):
    import lab3_1
elif(x==2):
    import lab3_2
else:
    import lab3_3

my question is : Do we have another way to build a menu without using if else? Why import doesn't work in python's switch case?

Gokul nath
  • 494
  • 2
  • 8
  • 17
  • Please use a descriptive title. – dspencer Mar 13 '20 at 09:48
  • 1
    What are you trying to accomplish here? Python does not have `switch` statement – Ach113 Mar 13 '20 at 09:49
  • 1
    Make use of dict (which is Python's equivalent of Java's Map) and map input with desired functions. Which is way better than switch/case – curiouscupcake Mar 13 '20 at 09:49
  • 1
    [What is the Python equivalent for a case/switch statement?](https://stackoverflow.com/q/11479816/1324033) – Sayse Mar 13 '20 at 09:49
  • Does this answer your question? [What is the Python equivalent for a case/switch statement?](https://stackoverflow.com/questions/11479816/what-is-the-python-equivalent-for-a-case-switch-statement) – Chayim Friedman Mar 13 '20 at 09:50
  • Does this answer your question? [Replacements for switch statement in Python?](https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) – dspencer Mar 13 '20 at 09:56

1 Answers1

0

Try below to import the required module.

mod_dict={1:'lab3_1', 2:'lab3_2' , 3:'lab3_3'
required_module=__import__(mod_dict[x])

Use required_module.operation() as your wish. For eg, required_module.add(), here the module will be imported under key word required_module.

Gokul nath
  • 494
  • 2
  • 8
  • 17