亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用字典調用方法時出現此錯誤(Python)

使用字典調用方法時出現此錯誤(Python)

aluckdog 2022-01-18 13:35:03
我想要做的是根據用戶放置程序的數字打開文件夾、瀏覽器或由字典關閉,其中每個數字都有一個功能。問題是它在所有情況下都返回 none 而不是 return 或函數。輸入 0 時應關閉程序。輸入 1 時,應打開 windows 7 的默認 .mp3。輸入 2 時,您應該只打開默認音樂文件夾。輸入 3 時,只需在屏幕上輸入“三”。最后通過輸入 666 使用我輸入的 URL 打開 google chrome。如果輸入另一個號碼,應留下“無效號碼” 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 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

您忘記從 中返回值switch_demo(),請檢查以下代碼:


 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()


查看完整回答
反對 回復 2022-01-18
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

您必須從switch_demo 另外返回一個函數,更改print(switch_demo(var)))為print(switch_demo(var)()). 可以這樣重寫以更有意義:


var = "something"

function = switch_demo(var)

print(function())

如果這是您想要的,這實際上會調用function并打印出它返回的任何內容。


查看完整回答
反對 回復 2022-01-18
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

字典值作為要調用的函數:


切換器[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")


查看完整回答
反對 回復 2022-01-18
  • 3 回答
  • 0 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號