【问题标题】:Printing calculated values in a list在列表中打印计算值
【发布时间】:2018-03-13 02:16:16
【问题描述】:

我是一名编程初学者(以前从未编程过),我被分配了一项任务(在学校),要编写一个程序,要求用户输入一个或多个形状,然后根据给出的尺寸计算该形状的体积用户。但是,如果用户没有输入“退出”,程序应该不断询问用户形状并继续计算体积,当用户输入“退出”时,程序应该打印出体积列表计算出来的。三种形状分别是立方体、金字塔和椭圆体。

例如,如果用户输入立方体、立方体、金字塔、金字塔、椭圆体然后退出(连同计算体积所需的尺寸),那么程序应该打印出: p>

立方体体积为 4、5

金字塔的体积是 6、7

椭球体积为 8

注意:这些数字只是举例。

我已经成功(有点)让程序注意到错误并让程序反复询问用户形状并计算体积,直到输入“退出”,但是我不知道如何实现“示例列表”类型的答案”,有没有办法做到这一点?

这是我的代码(它可能不是那么好,但它是我现在作为一个初学者所能做的最好的,并且用我们目前所学的东西):

import math

shapeName = input ("Please enter the shape you want to calculate the volumeof:").upper()

def volumeCube (side):
Volume = 0
Volume = side**3
print ("The Volume of the cube is", Volume)
return;

def volumePyramid (baseSideLength,height):
Volume = 0
Volume = round((1/3)*(baseSideLength**2)*height,1)
print ("The volume of the pyramid is", Volume)
return;

def volumeEllipsoid (radius1,radius2,radius3):
Volume = 0
Volume = round((4/3)*math.pi*radius1*radius2*radius3,1)
print ("The volume of the ellipsoid is", Volume)
return;

while shapeName != "QUIT":
    while shapeName in ["CUBE","PYRAMID","ELLIPSOID","QUIT"]:
        if shapeName == "CUBE":
           side = int(input("Please enter the length of the sides:"))
           volumeCube(side)
           shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
        elif shapeName == "PYRAMID":
           baseSideLength = int(input("Please enter the lengths of the side of the base:"))
           height = int(input("Please enter the height of the pyramid:"))
           volumePyramid(baseSideLength, height)
           shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
        elif shapeName == "ELLIPSOID":
           radius1 = int(input("Please enter the first radius:"))
           radius2 = int(input("Please enter the second radius:"))
           radius3 = int(input("Please enter the third radius:"))
           volumeEllipsoid(radius1, radius2, radius3)
           shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
        elif shapeName == "QUIT" :
           print ("\nYou have come to the end of the session. \nThe volumes calculated for each shape are shown below:")
           volumeCube (side)
           volumePyramid(baseSideLength, height)
           volumeEllipsoid(radius1, radius2, radius3)
           exit()
    else:
       print ("Error")
       shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
else:
   print ("\nyou have come to the end of the session. \nYou have not performed any volume calculations.")

这是我没有创建列表的原始代码,所以我尝试通过更改函数的“立方体”部分来测试它,例如:

def volumeCube (side):
    Volume = 0
    VolumeCubeList = []
    VolumeCubeList.append (side**3)
    print (VolumeCubeList)
    return;

但这只是返回一个答案(例如,如果第一个立方体的边长为 3,第二个立方体的边长为 4,则返回的答案仅为 [64]) 有没有办法来解决这个问题?还有什么我做错了吗?

【问题讨论】:

    标签: python python-3.x list loops nested-loops


    【解决方案1】:

    似乎问题是因为执行以下代码时未定义您的某些变量:

    elif shapeName == "QUIT" :
               print ("\nYou have come to the end of the session. \nThe volumes calculated for each shape are shown below:")
               volumeCube (side)
               volumePyramid(baseSideLength, height)
               volumeEllipsoid(radius1, radius2, radius3)
               exit()
    

    现在,为什么它不起作用?

    因为,例如,如果用户不想计算 volumePyramid,您仍然调用 volymePyramid(BaseSideLenght, height),basesideLenght 和 height 从未定义,因为它们从未由用户输入。 (其他形状也一样)

    你可以做的是有一个字符串列表,每次计算一个体积时存储一个体积,并在程序结束时显示这个列表;)

    这里是如何做到这一点:

    import math
    
    shapeName = input ("Please enter the shape you want to calculate the volume of:").upper()
    
    myList = []
    
    def volumeCube (side):
      Volume = 0
      Volume = side**3
      print ("The Volume of the cube is", Volume)
      myList.append("The volume of the cube was "+ str(Volume))
      return;
    
    def volumePyramid (baseSideLength,height):
      Volume = 0
      Volume = round((1/3)*(baseSideLength**2)*height,1)
      print ("The volume of the pyramid is", Volume)
      myList.append("The volume of the pyramid was "+ str(Volume))
      return;
    
    def volumeEllipsoid (radius1,radius2,radius3):
      Volume = 0
      Volume = round((4/3)*math.pi*radius1*radius2*radius3,1)
      print ("The volume of the ellipsoid is", Volume)
      myList.append("The volume of the ellipsoid was " + str(Volume))
      return;
    
    def printArray ():
      for word in myList:
        print(word)
    
    while shapeName != "QUIT":
        while shapeName in ["CUBE","PYRAMID","ELLIPSOID","QUIT"]:
            if shapeName == "CUBE":
               side = int(input("Please enter the length of the sides:"))
               volumeCube(side)
               shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
            elif shapeName == "PYRAMID":
               baseSideLength = int(input("Please enter the lengths of the side of the base:"))
               height = int(input("Please enter the height of the pyramid:"))
               volumePyramid(baseSideLength, height)
               shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
            elif shapeName == "ELLIPSOID":
               radius1 = int(input("Please enter the first radius:"))
               radius2 = int(input("Please enter the second radius:"))
               radius3 = int(input("Please enter the third radius:"))
               volumeEllipsoid(radius1, radius2, radius3)
               shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
            elif shapeName == "QUIT" :
               print ("\nYou have come to the end of the session. \nThe volumes calculated for each shape are shown below:")
               printArray()
               exit()
        else:
           print ("Error")
           shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
    else:
       print ("\nyou have come to the end of the session. \nYou have not performed any volume calculations.")
    

    【讨论】:

    • @Pengibaby:是的,当然;)我刚刚编辑了我的答案,让你更容易!如果有帮助别忘了点赞;)
    • 非常感谢您的帮助!我希望随着时间的推移改进我的编码,这真的很有帮助。我必须看起来像一个完整的菜鸟哈哈。任何方式,非常感谢你:) p.s。我尝试对答案进行投票,但是由于我刚刚加入 stackoverflow,因此我的声誉还不够高,无法显示..对不起:/
    • 很高兴 :) 如果有什么不清楚的地方请告诉我,我会澄清的!有美好的一天(或晚上):)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    相关资源
    最近更新 更多