2014-10-27 67 views
0

我有這個程序,我想要使用這些函數來請求高度,寬度和長度,並將它們作爲單獨的變量,以便我可以使用它們來繪製龜圖形。此外,使用我的getColor函數,我想要返回垃圾箱,並且可以單獨調色,以便我可以將它們應用於龜圖形。我對參數感到困惑;他們會在這裏有用嗎?返回函數中的變量

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') 
>>> 
+0

如果你想檢查範圍內的高度(40,61)和範圍內的寬度(40,61)和範圍內的長度(40,61)',該代碼是否沒有這樣做;你的if語句本質上是檢查高度,寬度和長度是否在範圍內(40,61) - 基本上你可能想要的長度,但是對於h和w,它只是檢查它們的值是否是'truthy' 。 – 2014-10-27 16:50:45

回答

0

你已經返回元組,所以你可以簡單的調用函數,並將其解壓到新的變量,而不是將它們打印的:

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

這幫助了很多,有誰知道我可以如何使用turtle圖形來使用getDim函數繪製帶有turtle圖形的bin的底部? – 2014-10-27 17:23:00

+0

@john diggle我不確定,我從來沒有用過烏龜。你最好在一個新的SO問題中提出這個問題(所以問題應該只關注一個想法)。同時儘量使用這個答案來解決它,然後發佈你在新問題中嘗試過的東西! – flakes 2014-10-27 17:28:56