问题描述
我有一个脚本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
指定前两个参数b
和c=1
,那么我将无法使用d=2
,因为脚本不包含带有2个参数的函数调用。解决方法是测量$:> julia foo.jl 1 2
中ARGS
的长度并有条件地调用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}}。