微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在条件下如何使函数使用默认参数

如何解决在条件下如何使函数使用默认参数

在这代码中,如果用户决定使用认值并单击Enter,则我想使该函数使用认参数。

但是,就我而言;它只是使用空字符串作为参数来运行代码

arc = ArcGIS()

def mapMaker(country,zoom = 4,location = "~/Desktop/",name = "Untitled"):
    loc = arc.geocode(country)
    countryLat = loc.latitude
    countryLon = loc.longitude
    map = folium.Map(location=[countryLat,countryLon],zoom_start=zoom,min_zoom=2)
    map.save("%s.html" % location + name)

mapZoom = input("Choose Your Map's Starting Zoom:(Default = 4) ")
mapLoc = input("Choose Where Your Map File Will Be Created:(Default = ~/Desktop) ")
mapName = input("Choose Your Map's Name:(Default = Untitled) ")
mapMaker(userin,zoom = mapZoom,location = mapLoc,name = mapName )

PS:我知道之前也曾问过类似的问题,但是由于此函数具有更多参数,因此我无法在此代码中使用它。

解决方法

我将使用字典而不是参数来实现这一点,这样,您可以随时将参数设置为默认值。看起来像这样:

def mapMaker(myDict):
    #access the arguments via mydict['your_argument']


myDict = {}
myDict['mapZoom'] = int(input("Choose Your Map's Starting Zoom:(Default = 4) ") or 4)})
myDict['mapLoc'] = str(input("Choose Where Your Map File Will Be Created:(Default = ~/Desktop) ") or '~/Desktop')
myDict['mapName'] = str(input("Choose Your Map's Name:(Default = Untitled) ") or 'Untitled')
mapMaker(myDict)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。