带有可选参数的Varargs函数

如何解决带有可选参数的Varargs函数

我有一个脚本import turtle import math import random import time screen = turtle.Screen() screen.title("Shape Shop") screen.setup(width=500,height=700) screen.bgcolor('lightsteelblue') turtleS = turtle.Turtle() turtleS.ht() turtleS.penup() turtleS.goto(0,250) turtleS.write("Welcome to the Shape Shop",align="center",font=("Avenir",18,"bold","underline")) pen = turtle.Turtle() pen.pensize(2) pen.ht() def instructions(): instPen = turtle.Turtle() instPen.ht() instPen.penup() instPen.goto(0,205) instPen.pendown() instPen.write("For this game,you can:",12)) instPen.penup() instPen.goto(-120,170) instPen.pendown() instPen.write("• Type in the name of a shape",12)) instPen.penup() instPen.goto(-100,145) instPen.pendown() instPen.write("- Names must be in lowercase",120) instPen.pendown() instPen.write("- Shape must have 12 or less sides",95) instPen.pendown() instPen.write("• Type in a number of sides",70) instPen.pendown() instPen.write("- Number must be less than 25",35) instPen.pendown() instPen.write(": The program will then draw the shape",10) instPen.pendown() instPen.write(": And tell you what shape it is",12)) instPen.penup() instPen.goto(-240,-340) instPen.pendown() instPen.write("Shape Shop v1.0",8)) instructions() def regShapeDraw(): length = ((1 / sides) * 500) angle = (360 / sides) pen.forward(length / 2) for i in range(sides - 1): pen.left(angle) pen.forward(length) pen.left(angle) pen.forward(length / 2) sides = [] def startUp(): mainGame() screen.listen() screen.onkeypress(startUp,"d") def mainGame(): userInput = screen.textinput("What shape do you want? ","Enter a shape name or a number of sides: ") if userInput == "COOL": eggPen = turtle.Turtle() eggPen.speed(0) eggPen.color('black') eggPen.penup() eggPen.goto(0,-80) eggPen.pendown() rotate=int(360) def drawCircles(t,size): size = 70 for i in range(5): t.circle(size) size=size-4 def drawSpecial(t,size,repeat): for i in range (repeat): drawCircles(t,size) t.right(360/repeat) drawSpecial(eggPen,100,10) shapeName = "WOAH!" else: if userInput.isalpha(): if userInput == "triangle": pen.penup() pen.goto(0,-300) pen.pendown() sides = 3 regShapeDraw() elif userInput == "square": pen.penup() pen.goto(0,-300) pen.pendown() sides = 4 regShapeDraw() elif userInput == "quadrilateral": pen.penup() pen.goto(0,-300) pen.pendown() sides = 4 regShapeDraw() elif userInput == "pentagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 5 regShapeDraw() elif userInput == "hexagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 6 regShapeDraw() elif userInput == "septagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 7 regShapeDraw() elif userInput == "heptagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 7 regShapeDraw() elif userInput == "octagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 8 regShapeDraw() elif userInput == "nonagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 9 regShapeDraw() elif userInput == "decagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 10 regShapeDraw() elif userInput == "hendecagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 11 regShapeDraw() elif userInput == "dodecagon": pen.penup() pen.goto(0,-300) pen.pendown() sides = 12 regShapeDraw() else: pen.penup() pen.goto(0,-250) pen.pendown() pen.write("We don't seem to have that shape in our database. You could try:",font=("Arial",12)) pen.penup() pen.goto(0,-275) pen.pendown() pen.write("» Making all the letters lowercase",-300) pen.pendown() pen.write("» Checking your spelling",-325) pen.pendown() pen.write("» Making sure the shape you want has 12 or less sides",12)) elif userInput.isnumeric(): if int(userInput) <= 25: pen.penup() pen.goto(0,-300) pen.pendown() sides = int(userInput) regShapeDraw() else: pen.penup() pen.goto(0,-250) pen.pendown() pen.write("We don't seem to have that shape in our database. You could try:",12)) pen.penup() pen.goto(0,-275) pen.pendown() pen.write("» Making the number less than 25",12)) else: pen.penup() pen.goto(0,-275) pen.pendown() pen.write("» Making your input all letters or all numbers",12)) if userInput == "3": shapeName = "Triangle" elif userInput == "4": shapeName = "Square" elif userInput == "5": shapeName = "Pentagon" elif userInput == "6": shapeName = "Hexagon" elif userInput == "7": shapeName = "Heptagon" elif userInput == "8": shapeName = "Octagon" elif userInput == "9": shapeName = "Nonagon" elif userInput == "10": shapeName = "Decagon" elif userInput == "11": shapeName = "Hendecagon" elif userInput == "12": shapeName = "Dodecagon" elif userInput == "13": shapeName = "Triskaidecagon" elif userInput == "14": shapeName = "Tetrakaidecagon" elif userInput == "15": shapeName = "Pentadecagon" elif userInput == "16": shapeName = "Hexakaidecagon" elif userInput == "17": shapeName = "Heptadecagon" elif userInput == "18": shapeName = "Octakaidecagon" elif userInput == "19": shapeName = "Enneadecagon" elif userInput == "20": shapeName = "Icosagon" elif userInput == "21": shapeName = "Icosikaihenagon" elif userInput == "22": shapeName = "Icosikaidigon" elif userInput == "23": shapeName = "Icosikaitrigon" elif userInput == "24": shapeName = "Icositetragon" elif userInput == "25": shapeName = "Icosikaipentagon" elif userInput == "triangle": shapeName = "Triangle" elif userInput == "square": shapeName = "Square" elif userInput == "quadrilateral": shapeName = "Square" elif userInput == "pentagon": shapeName = "Pentagon" elif userInput == "hexagon": shapeName = "Hexagon" elif userInput == "heptagon": shapeName = "Heptagon" elif userInput == "septagon": shapeName = "Heptagon" elif userInput == "octagon": shapeName = "Octagon" elif userInput == "nonagon": shapeName = "Nonagon" elif userInput == "decagon": shapeName = "Decagon" elif userInput == "hendecagon": shapeName = "Hendecagon" elif userInput == "dodecagon": shapeName = "Dodecagon" else: shapeName = "¯\(°_o)/¯" typeName = turtle.Turtle() typeName.ht() typeName.penup() typeName.goto(0,-120) typeName.pendown() typeName.write(shapeName,30,"bold")) screen.mainloop() ,其功能可以带4个参数,其中2个是可选的。我可以轻松地做到这一点

foo.jl

我可以使用终端中的参数调用

function bar(a,b,c=1,d=2)
    println(a,c,d)
end

bar(ARGS[1],ARGS[2],ARGS[3],ARGS[4])

但是,如果我只想用$:> julia foo.jl 1 2 3 4 1234 a指定前两个参数bc=1,那么我将无法使用d=2,因为脚本不包含带有2个参数的函数调用。解决方法是测量$:> julia foo.jl 1 2ARGS的长度并有条件地调用foo.jl

bar

但是,当超出4个参数时,这有点笨重。所以我研究了使用可变参数,在这里我可以用

调用任意数量的参数
if length(ARGS) == 2
    bar(ARGS[1],ARGS[2])
elseif length(ARGS) == 3
    bar(ARGS[1],ARGS[3])
else
    bar(ARGS[1],ARGS[4])
end

以多种方式调用

function bar(a,x...)
    println(a,x)
end

bar(ARGS[1],ARGS[3:end])

但是,如果终端中未提供$:> julia foo.jl 1 2 12(String[],) $:> julia foo.jl 1 2 3 4 12(["3","4"],) $:> julia foo.jl 1 2 3 4 5 6 12(["3","4","5","6"],) 的默认设置,我不知道如何(或者是否能够)设置。像x...这样的幼稚内容不起作用。此处的解决方案是根据function bar(a,x...=(1,2))的内容或大小在函数内设置变量。但我不知道是否有更好的方法可以做到这一点。

因此,我正在寻找一种使用终端中的参数调用函数的方法,其中需要一个数字(在这种情况下为2),而其余数字是可选的并设置为默认值。

解决方法

也许您正在寻找以下功能:

function flexiargs(a1,a2,a3="3",a4="4",a5="5",a6...)
    println((a1,a3,a4,a5,a6))
end
flexiargs(args::AbstractVector)=flexiargs(args...)

此函数将与任意数量的参数一起使用,前提是它们中至少有两个。

让我们测试以下数据:

args0=["one","two"]
args1=["one","two","three","four"];
args2=vcat(args,"five","six","seven") 

让我们看看它是如何工作的:

julia> flexiargs(args0)
("one","3","4","5",())

julia> flexiargs(args1)
("one","four",())

julia> flexiargs(args2)
("one",("six","seven"))

最后请注意,这也是可以的:

function flexiargs(a1,a6...=("6","7"))
    println((a1,a6))
end
flexiargs(args::AbstractVector)=flexiargs(args...)

在这种情况下,默认情况下(如果args中没有足够的值)a6将只是一个元素Tuple,而唯一的元素是Tuple {{ 1}}。

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

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-