【问题标题】:returning variables in function在函数中返回变量
【发布时间】:2014-10-27 16:31:37
【问题描述】:

我有这个程序,我想使用这些函数来询问高度、宽度和长度,并将它们作为单独的变量,以便我可以使用它们在海龟图形中绘制。同样使用我的 getColor 函数,我想返回 bin 并可以单独着色,以便我可以将它们应用到海龟图形中。我对参数有点困惑;它们在这里有用吗?

def main():

    print ("The bin dimensions are: ",(getDim()))
    numofcans = getCans()
    print ("You are recycling ",numofcans,"cans.")
    print("Your bin color is ",getColor())


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 and width 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 and width 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






main()

追溯:

>>> 
Enter the bins height (40 to 60): 45
Enter the bins width (40 to 60): 46
Enter the bins length (40 to 60): 47
The bin dimensions are:  (45, 46, 47)
Enter the amount of cans (10,1000): 101
You are recycling  101 cans.
Color menu 
 1 = 'blue' 
 2 = 'red' 
 3 = 'green' 
 4 = 'magenta' 
Pick the bin color: 1
Color menu 
 1 = 'blue' 
 2 = 'red' 
 3 = 'green' 
 4 = 'magenta' 
Pick the can color: 3
Your bin color is  ('blue', 'green')
>>> 

【问题讨论】:

  • 如果您尝试检查height in range(40,61) and width in range(40,61) and length in range(40,61),则该代码没有执行此操作;您的 if 语句本质上是检查高度、宽度和长度是否在范围内(40,61)——本质上是您可能想要的长度,但对于 h 和 w 它只是检查它们的值是否“真实” .

标签: python turtle-graphics


【解决方案1】:

类似

colors = getColor()

first = colors[0]
second = colors[1]

应该做的伎俩

希望我没有弄错,现在手头上没有 python。

另请参阅:How do you return multiple values in Python?

【讨论】:

    【解决方案2】:

    您已经作为元组返回,因此您可以简单地调用您的函数并将它们解压缩到新变量中,而不是打印它们:

    def main():
        height, width, length = getDim()
        numofcans = getCans()
        bincolor, cancolor = getColor()
        # go on to do rest of program
        ...
    

    【讨论】:

    • 这很有帮助,有谁知道我如何使用海龟图形通过 getDim 函数绘制带有海龟图形的垃圾箱底部?
    • @john diggle 我不确定,我从未使用过海龟。你最好在一个新的 SO 问题中问这个问题(SO 问题应该只关注一个想法)。也尽量使用这个答案自己解决它,然后在你的新问题中发布你尝试过的内容!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    相关资源
    最近更新 更多