2016-10-26 21 views
0

請注意我最近纔開始學習Python,所以尋找一些非常基本的代碼。如何列出所有計算的輸入?

我有一個程序需要計算三種形狀(立方體,金字塔和橢球體)的體積,這很容易。問題是,在測試人員(用戶)已經爲所有三種形狀計算了許多不同體積後,程序必須輸出不同形狀的列表以及它爲每個形狀計算的所有體積。

爲了更好的解釋,比方說用戶計算3個立方體,2個金字塔和5個橢圓體的體積。當在「退出」,而不是一個新的形狀,然後程序的用戶類型必須輸出:

「你已經來到會議結束 計算每個形狀的卷如下: 立方:(所有體積清單計算) 金字塔:(所有體積清單計算) 橢圓體:(所有體積清單計算)「

謝謝您提前! 對於使用Pycharm埃杜3.0.1和Python 3.5.7

while True : 
shape =input("Please enter the shape you wish to find the volume of: ")      
if shape == "cube":                   
    sideLength = int(input("Please enter side Length: ")) 
    def main():                    
     result1 = round(cubeVolume(sideLength),2) 
     print ("A cube with side length {} has a volume {}".format(sideLength,result1)) 
    def cubeVolume(sideLength):                
     volume = sideLength**3 
     return volume 
    main() 
    continue                     
elif shape == "pyramid":                  
    baseLength = int(input("Please enter the base length: "))         
    heightLength = int(input("Please enter the height: ")) 
    def main():                    
     result2 = round((pyramidVolume(baseLength,heightLength)),2) 
     print ("A pyramid with base {} and height {} has a volume {}".format(baseLength,heightLength,result2)) 
    def pyramidVolume(baseLength, heightLength):            
     volume = ((1/3)*(baseLength**2)*(heightLength)) 
     return volume 
    main()                     
    continue                     
elif shape == "ellipsoid":                 
    r1 = int(input("Please enter the longest radius: ")) 
    r2 = int(input("Please enter the smaller radius: ")) 
    r3 = int(input("Please enter the third radius: ")) 
    import math                    
    def main():                    
     result3 = round(ellipsoidVolume(r1,r2,r3),2) 
     print ('An ellipsoid with a radius of {}, {}, and {} has a volume {}'.format(r1,r2,r3,result3)) 
    def ellipsoidVolume(r1,r2,r3):               
     volume = ((4/3)*(math.pi))*r1*r2*r3 
     return volume 
    main() 
    continue                     
elif shape == "quit":                   
    print ("You have come to the end of the session.") 
    print ("The volumes calculated for each shape are shown below.") 
    print ("Cube: ") #what I need to find out 
    print ("Pyramid: ") #same as above 
    print ("Ellipsoid: ") # again 
else:                      
    print ("That is not a proper shape") 
    continue 

回答

0

我會做這個用3只列出了記錄。所以,你的while(True)之前-loop您定義的列表:

cubeList = [] 
pyramidList = [] 
ellipsoidList = [] 

然後在你的3卷的主要功能,你可以將結果添加到列表中。例如,對於您將有以下主要功能金字塔:

def main(): 
    result2 = round((pyramidVolume(baseLength,heightLength)),2) 
    pyramidList.append(result2) 
    print ("A pyramid with base {} and height {} has a volume {}".format(baseLength,heightLength,result2)) 

現在,每個用戶計算量的時候,就會被添加到3所列出之一。作爲最後一步,你可以只打印清單,而用戶想退出:

print ("Cube: {}".format(cubeList)) 
print ("Pyramid: {}".format(pyramidList)) 
print ("Ellipsoid: {}".format(ellipsoidList)) 

如果你想有很好的清單格式化,你可以做這樣的事情:

cs = "" 
for ind,el in enumerate(cubeList): 
    cs += "Shape {} has volume {}".format(ind,el) 
print ("Cube: {}".format(cs))