Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Help with Python Task please

Python nested list programming Help Loop

  • Please log in to reply

#1
Student1122

Student1122

    New Member

  • Member
  • Pip
  • 2 posts

TASK: Calculate surface and volume of a cylinder

rhList = [[4.276, 7.000], [4.000, 8.000], [3.771, 9.000], \
[4.443, 3.142], [3.142, 6.284], [2.565, 9.425], [2.221, 12.566]]
rhList is given
Write a loop that does the following things:

Get the values for r and h from rhList:
Calculate Surface and Volume of a cylinder
Write out r, h, S og V with 3 decimales accuracy.
Save r, h, S og V in a nested list called resultList.

It should look like this when its done:
[[4.276, 7.0, 302.951168611585, 402.09003098979963],
[4.0, 8.0, 301.59289474462014, 402.1238596594935],
[3.771, 9.0, 302.5946920931822, 402.07349678766377],
[4.443, 3.142, 211.74431874567463, 194.85371033115726],
[3.142, 6.284, 186.08590738460174, 194.8939736674729],
[2.565, 9.425, 193.23528005185852, 194.80763343051234],
[2.221, 12.566, 206.35191318586456, 194.7350135716788]]


Edited by Student1122, 11 February 2021 - 04:31 PM.

  • 0

Advertisements


#2
RKinner

RKinner

    Malware Expert

  • Expert
  • 23,549 posts
  • MVP

I don't think anyone is going to do your homework for you but if you  post the code you have so far we would be glad to help debug it.


  • 0

#3
Student1122

Student1122

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts

My first try is:

rhList = [[4.276, 7.000], [4.000, 8.000], [3.771, 9.000], \
[4.443, 3.142], [3.142, 6.284], [2.565, 9.425], [2.221, 12.566]]

ListO=[2*pi*r**2+2*pi*r*h for r,h in rhList]
ListV=[pi*r**2*h for r,h in rhList]
resultlist=[[O,V,r,h] for O,V in zip (ListO,ListV)]
resultlist

Outcome :
[[302.951168611585, 402.09003098979963, 2.221, 12.566],
[301.59289474462014, 402.1238596594935, 2.221, 12.566],
[302.5946920931822, 402.07349678766377, 2.221, 12.566],
[211.74431874567463, 194.85371033115726, 2.221, 12.566],
[186.08590738460174, 194.8939736674729, 2.221, 12.566],
[193.23528005185852, 194.80763343051234, 2.221, 12.566],
[206.35191318586456, 194.7350135716788, 2.221, 12.566]]

I dont know how to get r,h from rhList to also run the loop. They stay the same, also how do i only get 3 decimales in this case?

my second attempt:

rhList = [[4.276, 7.000], [4.000, 8.000], [3.771, 9.000], \
[4.443, 3.142], [3.142, 6.284], [2.565, 9.425], [2.221, 12.566]]

for r,h in rhList:
O=2*pi*r**2+2*pi*r*h
V=pi*r**2*h


print ('{0:0.3f} {1:7.3f} {2:10.3f} {3:15.3f}'.format(O,V,r,h))

outcome:
302.951 402.090 4.276 7.000
301.593 402.124 4.000 8.000
302.595 402.073 3.771 9.000
211.744 194.854 4.443 3.142
186.086 194.894 3.142 6.284
193.235 194.808 2.565 9.425
206.352 194.735 2.221 12.566

Now the results are correct, but they dont come out as an nested list.


  • 0

#4
RKinner

RKinner

    Malware Expert

  • Expert
  • 23,549 posts
  • MVP

In your first example could you not do something like:

ListR=r

ListH=h

ListO=[Round(2*pi*r**2+2*pi*r*h for r,h in rhList,3)]
ListV=[Round(pi*r**2*h for r,h in rhList,3)]

 

Then create your ResultList from R, H, O & V

 

Note your output isn't in the order required by the original problem statement.  Should start with radius, height


  • 0






Similar Topics


Also tagged with one or more of these keywords: Python, nested list, programming, Help, Loop

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP