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

Python 如何使函数在条件下使用默认参数,python,python-3.x,function,Python,Python 3.x,Function,在这段代码中,如果用户决定使用默认值并单击enter,我想让函数使用默认参数,这会导致输入为空字符串 然而,就我而言;它只是使用空字符串作为参数运行代码 arc = ArcGIS() def mapMaker(country, zoom = 4, location = "~/Desktop/", name = "Untitled"): loc = arc.geocode(country) countryLat = loc.latitude

在这段代码中,如果用户决定使用默认值并单击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:我知道以前也有人问过类似的问题,但我无法使用此代码,因为此函数有更多的参数。

我会使用字典而不是参数来处理此问题,这样,您可以在运行时将参数设置为默认值。它看起来是这样的:

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

这回答了你的问题吗?对最后,我能够让代码正常工作。