2014-10-27 69 views
0

簡單的修復,但我怎樣才能使這個停止循環?輸出結果只是反覆詢問數值。還有一種方法可以檢查所有的值以確保在20到40之間?定義的函數重複循環

def main():  
    print("Welcome to the autostacker") 
    print(getDim()) 

def getDim(): 
    height = int(input("Enter the height: ")) 
    width = int(input("Enter the width: ")) 
    length = int(input("Enter the width: ")) 
    return getDim() 


main() 
+1

更改'返回getDim()'爲'返回高度,重量,長度' – ssm 2014-10-27 05:38:29

+0

您想如何顯示正在輸入的字段?你目前擁有的是一個永無止境的遞歸循環。 – 2014-10-27 05:40:44

+1

[20 2014-10-27 05:41:45

回答

0

喜歡的東西

def main():  
    print("Welcome to the autostacker") 
    h, w, l = getDim() 

    if 20 <= h <= 40: 
     if 20 <= w <= 40: 
      if 20 <= l <= 40: 
       print "height is %d width is %d length is %d" % (h, w, l) 
      else: 
       print 'length out of range' 
     else: 
      print 'width out of range' 
    else: 
     print 'height out of range' 




def getDim(): 
    height = int(input("Enter the height: ")) 
    width = int(input("Enter the width: ")) 
    length = int(input("Enter the length: ")) 
    return height, width, length 

main() 

確保你不叫getDim內getDim除非你有一些條件,最終使之停止自稱。

+0

好吧,我現在明白了,非常感謝你的幫助 – 2014-10-27 06:05:49

0

getDim()你return語句是造成循環,我想了一個。您可以檢查值是否在範圍內,如下所示。

def main():  
    print("Welcome to the autostacker") 
    print(getDim()) 

def getDim(): 
    height = int(input("Enter the height: ")) 
    width = int(input("Enter the width: ")) 
    length = int(input("Enter the width: ")) 
    if 20 <= height <=40 and 20 <= width <= 40 and 20 <= length <= 40: 
     print "all the values lie in the range" 
     # DO WHAT YOU WANT 
    else: 
     print " values are not in range" 
     # DO SOMETHING ELSE 
    return height, width,length 

main()