如果用户输入“”,如何创建默认值?

时间:2019-03-02 04:02:55

标签: 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("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)

如何添加参数以将此默认值设置为250?该代码有效,但是当输入空值时无效。因为是上学,所以我不能删除main函数中的if / else语句。我敢肯定答案是显而易见的,但是我是python的新手,所以我不知道如何解决。

0 个答案:

没有答案