What I want to do is that depending on a number that the user puts the program open a folder, the browser, or closed by a dictionary in which each number makes a function. The problem is that it returns a none in all cases and not the return or the function.

When entering 0 the program should be closed.

When entering 1, a default .mp3 of windows 7 should be opened.

When entering 2 you should only open the default music folder.

When entering 3 just put "three" on the screen.

And finally by entering 666 that the google chrome was opened with the URL that I put.

In case of putting another number should leave "Invalid num"

 import webbrowser
 import subprocess
 import sys
 opened = True

   def  one():

      print("Opening explorer.exe")
      #subprocess.Popen(r'explorer /select,"C:\Users\reciclo"')
      subprocess.call("explorer C:\\Users\\Public\\Music\\Sample 
      Music\Kalimba.mp3", shell=True)
      return "opened"
   def zero():

      print("Exit the program")
      opened = False
      return "Exit"
   def two():

      subprocess.call("explorer C:\\Users\\Public\\Music\\Sample Music", 
      shell=True)

      return "two"
  def three():


      return "three"
  def demon():

      demon_url = 'https://piv.pivpiv.dk/'
      chrome_path = 'C:/Program Files 
      (x86)/Google/Chrome/Application/chrome.exe %s'
      webbrowser.get(chrome_path).open(demon_url)

      return "invoked"
  def switch_demo(var):

  switcher = {
            0: zero,
            1: one,
            2: two,
            3: three,

           666: demon,

  }
   var = switcher.get(var, "Invalid num")
   while opened:
 if opened == True:
  var = int(input("enter a number between 1 and 9999999999 "))
  print(switch_demo(var)))

 elif opened== False:
  print("Goout")
  sys.exit()

3 Answers

0
ncica On

Dictionary value as function to be called:

switcher[0]()

def switch_demo(var):
    switcher = {
        0: zero,
        1: one,
        2: two,
        3: three,

        666: demon,
    }
    #var = switcher.get(var, "Invalid num")
    switcher[int(var)]() # exec function

-

def switch_demo(var):
    switcher = {
        0: zero,
        1: one,
        2: two,
        3: three,

        666: demon,
    }
    #var = switcher.get(var, "Invalid num")
    return (switcher[int(var)]())

while opened:

    if opened == True:
        var = int(input("enter a number between 1 and 9999999999 "))
        print (switch_demo(var))

    elif opened == False:
        print("Goout")
0
Mahmoud Elshahat On

you forgot to return a value from switch_demo(), check code below:

 def switch_demo(var):

    switcher = {
            0: zero,
            1: one,
            2: two,
            3: three,

           666: demon,

    }
    return switcher.get(var, "Invalid num")


while opened:
    if opened == True:
        var = int(input("enter a number between 1 and 9999999999 "))
        print(switch_demo(var)))

    elif opened== False:

print("Goout")
sys.exit()
0
Community On

@Mahmoud Elshahat is right that you must return a function from switch_demo Also, change print(switch_demo(var))) into print(switch_demo(var)()). It could be rewritten like this to make more sense:

var = "something"
function = switch_demo(var)
print(function())

This will actually call function and also print out whatever it returns, if that is what you wanted.