Python 如果用户输入“如何创建默认值”&引用;?
我的学校程序需要一个棋盘,如果用户不输入任何内容,程序默认为一个设定值。在这种情况下是250。这是我的密码Python 如果用户输入“如何创建默认值”&引用;?,python,python-3.x,Python,Python 3.x,我的学校程序需要一个棋盘,如果用户不输入任何内容,程序默认为一个设定值。在这种情况下是250。这是我的密码 from chessboard import drawChessboard import turtle t = turtle.Turtle() #main function def main(): #input width = input("Please enter the starting X-coordinate: ") height = input("Pl
from chessboard import drawChessboard
import turtle
t = turtle.Turtle()
#main function
def main():
#input
width = input("Please enter the starting X-coordinate: ")
height = input("Please enter the starting Y-Coordinate: ")
startX = int(input("Please enter the width of the chessboard: "))
startY = int(input("Please enter the height of the chessboard: "))
if width == "" and height == "":
drawChessboard(startX, startY)
elif height == "":
drawChessboard(startX, startY, width=eval(width))
elif width == "":
drawChessboard(startX, startY, height=eval(height))
else:
drawChessboard(startX, startY, eval(width), eval(height))
main()
turtle.done()
这是我在pycharm中的主要文件,并附有第二个
import turtle
t = turtle.Turtle()
def drawRectangles(widthd, heightd, startXd, startYd):
t.pendown()
t.color("black")
t.begin_fill()
t.left(90)
t.forward(heightd / 8)
t.right(90)
t.forward(widthd / 8)
t.right(90)
t.forward(heightd / 8)
t.right(90)
t.forward(widthd / 8)
t.right(180)
t.end_fill()
t.penup()
if startXd and startYd == 0:
t.color("black")
def drawAllRectangles(widthd, heightd, startXd, startYd):
row1 = 0
while row1 < 8:
t.goto(startXd + (widthd / 8) * row1, startYd)
drawRectangles(widthd, heightd, startXd, startYd )
row1 += 2
row2 = 1
while row2 < 8:
t.goto(startXd + (widthd / 8) * row2, startYd + (heightd / 8))
drawRectangles(widthd, heightd, startXd, startYd)
row2 += 2
row3 = 0
while row3 < 8:
t.goto(startXd + (widthd / 8) * row3, startYd + (heightd / 8) * 2)
drawRectangles(widthd, heightd, startXd, startYd)
row3 += 2
row4 = 1
while row4 < 8:
t.goto(startXd + (widthd / 8) * row4, startYd + (heightd / 8) * 3)
drawRectangles(widthd, heightd, startXd, startYd)
row4 += 2
row5 = 0
while row5 < 8:
t.goto(startXd + (widthd / 8) * row5, startYd + (heightd / 8) * 4)
drawRectangles(widthd, heightd, startXd, startYd)
row5 += 2
row6 = 1
while row6 < 8:
t.goto(startXd + (widthd / 8) * row6, startYd + (heightd / 8) * 5)
drawRectangles(widthd, heightd, startXd, startYd)
row6 += 2
row7 = 0
while row7 < 8:
t.goto(startXd + (widthd / 8) * row7, startYd + (heightd / 8) * 6)
drawRectangles(widthd, heightd, startXd, startYd)
row7 += 2
row8 = 1
while row8 < 8:
t.goto(startXd + (widthd / 8) * row8, startYd + (heightd / 8) * 7)
drawRectangles(widthd, heightd, startXd, startYd)
row8 += 2
def drawChessboard(widthd, heightd, startXd, startYd):
t.color("red")
t.penup()
t.goto(startXd, startYd)
t.pendown()
t.goto(widthd + startXd, startYd)
t.goto(widthd + startXd, heightd + startYd)
t.goto(startXd, heightd + startYd)
t.goto(startXd, startYd)
t.color("black")
drawAllRectangles(widthd, heightd, startXd, startYd)
导入海龟
t=海龟。海龟()
def绘图矩形(宽度、高度、起点、起点):
t、 彭敦()
t、 颜色(“黑色”)
t、 开始填充()
t、 左(90)
t、 前进(高度/8)
t、 右(90)
t、 向前(宽度D/8)
t、 右(90)
t、 前进(高度/8)
t、 右(90)
t、 向前(宽度D/8)
t、 右(180)
t、 (完)
t、 彭普()
如果startXd和startYd==0:
t、 颜色(“黑色”)
def DRAWALL矩形(宽度、高度、起点、起点):
第1行=0
当第1行小于8行时:
t、 转到(起始xd+(宽度d/8)*第1行,起始d)
绘图矩形(宽度、高度、起点、起点)
行1+=2
第2行=1
当第2行小于8行时:
t、 转到(起始xd+(宽度d/8)*第2行,起始d+(高度d/8))
绘图矩形(宽度、高度、起点、起点)
行2+=2
第3行=0
当第3行小于8行时:
t、 转到(起始xd+(宽度/8)*第3行,起始d+(高度/8)*2)
绘图矩形(宽度、高度、起点、起点)
行3+=2
第4行=1
当第4行小于8行时:
t、 转到(起始xd+(宽度/8)*行4,起始d+(高度/8)*3)
绘图矩形(宽度、高度、起点、起点)
行4+=2
第5行=0
当第5行小于8行时:
t、 转到(起始xd+(宽度/8)*第5行,起始d+(高度/8)*4)
绘图矩形(宽度、高度、起点、起点)
行5+=2
第6行=1
当第6行小于8行时:
t、 转到(起始xd+(宽度/8)*第6行,起始d+(高度/8)*5)
绘图矩形(宽度、高度、起点、起点)
第6行+=2
第7行=0
当第7行小于8行时:
t、 转到(起始xd+(宽度/8)*第7行,起始d+(高度/8)*6)
绘图矩形(宽度、高度、起点、起点)
第7行+=2
第8行=1
当第8行小于8行时:
t、 转到(起始xd+(宽度/8)*第8行,起始d+(高度/8)*7)
绘图矩形(宽度、高度、起点、起点)
第8行+=2
def绘图棋盘(宽度、高度、起点、起点):
t、 颜色(“红色”)
t、 彭普()
t、 转到(startXd,startYd)
t、 彭敦()
t、 goto(宽度D+startXd,startYd)
t、 goto(宽度+标准尺寸,高度+标准尺寸)
t、 转到(起始xd,高度+起始d)
t、 转到(startXd,startYd)
t、 颜色(“黑色”)
绘制所有矩形(宽度、高度、起点、起点)
我如何添加一个参数使其默认为250?该代码有效,但当输入的值为nothing时无效。因为它是针对学校的,所以我不能删除主函数中的if/else语句。我确信答案是显而易见的,但我是python新手,所以我不知道如何解决它。
variable=input(“…”)或default_value
会起作用,因为p或q
相当于p如果p或者q
。不要使用eval
。你想要一个int
,所以使用width=int(width)
variable=input(“…”)或默认值将起作用,因为p或q
相当于p如果p或者q
。不要使用eval
。您需要一个int
,所以使用width=int(width)
。