0

I’m currently having trouble formatting output for this project/assignment (python 3.8).

I have to prompt the user to input either a cube, pyramid, or ellipsoid and calculate the volume of the inputted shape. After the user has calculated many different volumes for all 3 shapes, the program has to output a list of the different shapes and all the volumes it has calculated for each shape using a list. This list has to be a tuple in the form of (shapename, volume). For instance, the list might look like: [(“cube”,1.00),(“cube”,9.00)]. I also have to sort the list from the lowest to highest using the values of the volumes.

My output looks like this & it's almost correct, I just have to figure out a way to remove the parentheses, quotation marks, and commas from the bottom 4 lines that display the shape name and the corresponding volume.

Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e): pyramid
Enter the base of the pyramid: 12
Enter the height of the pyramid: 16
The volume of a pyramid with base 12.0 and height 16.0 is:   768.00

Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e): p
Enter the base of the pyramid: 4
Enter the height of the pyramid: 8
The volume of a pyramid with base 4.0 and height 8.0 is:   42.67

Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e): cube
Enter length of side for the cube: 11
The volume of a cube with side 11.0 is:   1331.00

Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e): e
Enter the first radius: 7
Enter the second radius: 8
Enter the third radius: 9
The volume of an ellipsoid with radii 7.0 and 8.0 and 9.0 is:   2111.15

Please enter shape (quit/q, cube/c, pyramid/p, ellipsoid/e): quit
Output: Volume of shapes entered in sorted order:
('pyramid', 42.67)
('pyramid', 768.0)
('cube', 1331.0)
('ellipsoid', 2111.15)

for reference, here is the part of my code that is supposed to print each of the items/tuples in my list:

elif shapeName == "quit" or shapeName == "q" :
        print ("Output: Volume of shapes entered in sorted order:")
        myList.sort(key = lambda myList: myList[1])
        for item in myList:
                print (item)
        exit()
  • Maybe this? `print(f"item[0]} {item[1]}")` – nocibambi Oct 15 '20 at 18:01
  • You can index into the tuples and use [string formatting](https://stackoverflow.com/questions/13945749/string-formatting-in-python-3) to print in any format you want. What have you tried so far? – G. Anderson Oct 15 '20 at 18:01

2 Answers2

2

I think this could help you...

for item in myList:
    print (*item, sep=" : ")
Anurag Singh
  • 126
  • 5
0

You can extract the values of each item, and print it:

for item in l:
    shapename, volume = item
    print(f"{shapename}, {volume}")  

Output:

pyramid, 42.67
pyramid, 768.0
cube, 1331.0
ellipsoid, 2111.15

If you don't care about the comma, you can also try this:

for item in l:
    print(*item)

Output

pyramid 42.67
pyramid 768.0
cube 1331.0
ellipsoid 2111.15
Meny Issakov
  • 1,400
  • 1
  • 14
  • 30