【问题标题】:turtle graphics with functions带函数的海龟图形
【发布时间】:2014-12-23 00:12:02
【问题描述】:

我卡住了,我需要使用从 getDim() 获得的宽度和长度在海龟图形中绘制一个 bin,然后将颜色应用于来自 getColor 的 can 和 bin。我不明白如何在我的 getDraw 函数中调用这些函数,而无需用户重复输入。我还需要用给定长度的罐子来填满垃圾箱的底行,我很少使用海龟图形,所以我迷路了。

def main():
    candiam = 2.5
    height, width, length = getDim()
    numofcans = getCans()
    bincolor, cancolor = getColor()
    print ("The bin dimensions are: ",height," inches high", width," inches wide and", length," inches long")
    print ("You are recycling ",numofcans," cans.")



def getDim():

    height = int(input("Enter the bins height (40 to 60): "))
    width = int(input("Enter the bins width (40 to 60): "))
    length = int(input("Enter the bins length (40 to 60): "))
    while height not in range(40,61) and width not in range(40,61) and length not in range(40,61):
        print("You entered a wrong value")
        height = int(input("Enter the height (40 to 60: "))
        width = int(input("Enter the width(40 to 60: "))
        length = int(input("Enter the length (40 to 60: "))
    if height in range(40,61) and width in range(40,61) and length in range(40,61):
        return height, width, length

def getCans():

    cans = int(input("Enter the amount of cans (10,1000): "))
    if cans in range(10,1001):
        return cans
    while cans not in range(10,1001):
        cans = int(input("Invalid number, please enter the amount of cans (10,1000): "))
    return cans    

def getColor():
    bincolor = int(input("Color menu \n 1 = 'blue' \n 2 = 'red' \n 3 = 'green' \n 4 = 'magenta' \nPick the bin color: "))
    while bincolor not in range(1,5):
        bincolor = int(input("Color menu \n 1 = 'blue' \n 2 = 'red' \n 3 = 'green' \n 4 = 'magenta' \nPick the bin color: "))
    while bincolor in range(1,5):
        if bincolor == 1:
            bincolor = "blue"
        elif bincolor == 2:
            bincolor = "red"
        elif bincolor == 3:
            bincolor = "green"
        elif bincolor == 4:
            bincolor = "magenta"

    cancolor = int(input("Color menu \n 1 = 'blue' \n 2 = 'red' \n 3 = 'green' \n 4 = 'magenta' \nPick the can color: "))
    while cancolor not in range(1,5):
        cancolor = int(input("Color menu \n 1 = 'blue' \n 2 = 'red' \n 3 = 'green' \n 4 = 'magenta' \nPick the can color: "))

    while cancolor in range(1,5):
        if cancolor == 1:
            cancolor = "blue"
        elif cancolor == 2:
            cancolor = "red"
        elif cancolor == 3:
            cancolor = "green"
        elif cancolor == 4:
            cancolor = "magenta"
        return bincolor, cancolor


def drawBin():


main()

【问题讨论】:

  • 你的问题到底是什么?

标签: python turtle-graphics


【解决方案1】:

保存返回的颜色和返回的尺寸并将它们传递回绘图箱...getColorgetDim 都明确地从用户那里获取输入...这就是他们所做的一切。 .. 如果不是用户输入,我不确定你会期待什么

getColor 中的第二个 while 循环也不使用它

def getColor():
    bincolor = int(input("Color menu \n 1 = 'blue' \n 2 = 'red' \n 3 = 'green' \n 4 = 'magenta' \nPick the bin color: "))
    while bincolor not in range(1,5):
        bincolor = int(input("Color menu \n 1 = 'blue' \n 2 = 'red' \n 3 = 'green' \n 4 = 'magenta' \nPick the bin color: "))

    if bincolor == 1:
        bincolor = "blue"
    elif bincolor == 2:
        bincolor = "red"
    elif bincolor == 3:
        bincolor = "green"
    elif bincolor == 4:
        bincolor = "magenta"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多