Forum Settings
Forums
New
Sep 18, 2017 12:53 PM
#1

Offline
Nov 2016
640
I know this isn't the place to ask, but I'm desperate and stupid. So here goes:

I'm trying to create an empty list using a for-loop that calculates the ratio for the different radiuses, and adds it to the empty list.


from pylab import *

pi=3.14
radiuses=[0.5,1,1.5,2,2.5,3,3.5,4]
volume = 4/3 * pi * radiuses**3
surface_area = 4 * pi * radiuses**2
print (surface_area)

ratios= []


for number in range(8)
ratio = round(surface_area / volume,2)
ratios.append (number)
"In this world, evil can arise from the best of intentions. And there is good which can come from evil intentions"
Sep 18, 2017 2:21 PM
#2

Offline
Jun 2014
22470
I wouldn't waste my time with the Python 3.

Just get a normal Genesis controller instead.

Sep 18, 2017 3:02 PM
#3
Offline
Jul 2016
852
I don't have pylab handy and I don't feel like redownloading it, so I can't comment on whether you're using its (list * int -> list) operators correctly. However, assuming you have those right and you're indenting properly, your only mistake is that you forgot to put a colon on the end of the for statement.

Also, you probably meant to append "ratio" to ratios, rather than "number".

Without using pylab's list operators, I believe this is what you're looking for (indent where appropriate, since MAL apparently doesn't like leading spaces):

# Constant
pi=3.14

# Initialize lists
radiuses=[0.5,1,1.5,2,2.5,3,3.5,4] # Just FYI, the correct plural form is "radii."
volume=[]
surface_area=[]

# Calculate volumes and surface areas
for rad in radiuses:
volume += [4/3 * pi * rad**3]
surface_area += [4 * pi * rad**2]
# Print surface areas
print (surface_area)

# Initialize ratio list
ratios= []

# For i in range [0,8), calculate the value of surface_area[i] / volume[i], round to two decimal places, and add it to the end of the ratio list.
# SUGGESTION: Consider changing the "8" to "len(radiuses)" so that you only have to change one thing
# if you decide to make "radiuses" longer or shorter.
for number in range(8):
ratio = round(surface_area[number] / volume[number],2)
ratios.append (ratio)

# Print the ratio list (assuming you want to do this)
print(ratios)

Edit: Closed off code in a quote box and added comments.
PhendrusSep 18, 2017 3:14 PM
Important Note: I no longer - in any way, shape, or form - consider myself a moral nihilist (even in my old, convoluted definition of the term). I very much do believe there is such a thing as objective good and evil. In addition, I apologize for any of the posts I've made that are rude, aggressive, or otherwise unbecoming.

I've always striven to walk a path befitting a follower of Christ, and now recognize some of my old comments here as misguided if not outright wrong. If you happen upon them, pray do not let them darken your view of the God I serve. He is kind, even if I, at times, have not been.
Sep 19, 2017 9:40 AM
#4

Offline
Jan 2008
173
Mayling said:

I'm trying to create an empty list using a for-loop that calculates the ratio for the different radiuses, and adds it to the empty list.


from pylab import *

pi=3.14
radiuses=[0.5,1,1.5,2,2.5,3,3.5,4]
volume = 4/3 * pi * radiuses**3
surface_area = 4 * pi * radiuses**2
print (surface_area)

ratios= []


for number in range(8)
ratio = round(surface_area / volume,2)
ratios.append (number)

ratios = [] is the correct way to create an empty list, only you're filling it with the iterator from the for loop instead of the ratio variable.

Here's a shorter way you could write this:

#!/usr/bin/env python
import math

radius = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
result = []

def findratio(x):
    return round((4 * math.pi * x**2) / (4 / 3 * math.pi * x**3), 2) #kinda ugly

for i in radius:
    result.append(findratio(i))

print(result)

Running the program:

$ ./spherething.py
[6.0, 3.0, 2.0, 1.5, 1.2, 1.0, 0.86, 0.75]
Sep 20, 2017 10:56 AM
#5

Offline
Nov 2016
640
Zuggy said:
Mayling said:

I'm trying to create an empty list using a for-loop that calculates the ratio for the different radiuses, and adds it to the empty list.


from pylab import *

pi=3.14
radiuses=[0.5,1,1.5,2,2.5,3,3.5,4]
volume = 4/3 * pi * radiuses**3
surface_area = 4 * pi * radiuses**2
print (surface_area)

ratios= []


for number in range(8)
ratio = round(surface_area / volume,2)
ratios.append (number)

ratios = [] is the correct way to create an empty list, only you're filling it with the iterator from the for loop instead of the ratio variable.

Here's a shorter way you could write this:

#!/usr/bin/env python
import math

radius = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
result = []

def findratio(x):
    return round((4 * math.pi * x**2) / (4 / 3 * math.pi * x**3), 2) #kinda ugly

for i in radius:
    result.append(findratio(i))

print(result)

Running the program:

$ ./spherething.py
[6.0, 3.0, 2.0, 1.5, 1.2, 1.0, 0.86, 0.75]


That looks a lot better! Thank youuuuuu :D
"In this world, evil can arise from the best of intentions. And there is good which can come from evil intentions"
Sep 20, 2017 10:57 AM
#6

Offline
Nov 2016
640
T
Phendrus said:
I don't have pylab handy and I don't feel like redownloading it, so I can't comment on whether you're using its (list * int -> list) operators correctly. However, assuming you have those right and you're indenting properly, your only mistake is that you forgot to put a colon on the end of the for statement.

Also, you probably meant to append "ratio" to ratios, rather than "number".

Without using pylab's list operators, I believe this is what you're looking for (indent where appropriate, since MAL apparently doesn't like leading spaces):

# Constant
pi=3.14

# Initialize lists
radiuses=[0.5,1,1.5,2,2.5,3,3.5,4] # Just FYI, the correct plural form is "radii."
volume=[]
surface_area=[]

# Calculate volumes and surface areas
for rad in radiuses:
volume += [4/3 * pi * rad**3]
surface_area += [4 * pi * rad**2]
# Print surface areas
print (surface_area)

# Initialize ratio list
ratios= []

# For i in range [0,8), calculate the value of surface_area[i] / volume[i], round to two decimal places, and add it to the end of the ratio list.
# SUGGESTION: Consider changing the "8" to "len(radiuses)" so that you only have to change one thing
# if you decide to make "radiuses" longer or shorter.
for number in range(8):
ratio = round(surface_area[number] / volume[number],2)
ratios.append (ratio)

# Print the ratio list (assuming you want to do this)
print(ratios)

Edit: Closed off code in a quote box and added comments.


Thank you soo much :D
"In this world, evil can arise from the best of intentions. And there is good which can come from evil intentions"

More topics from this board

» What are you playing right now? (v2) ( 1 2 3 4 5 ... Last Page )

anime-prime - Oct 4, 2020

3522 by Kenrin »»
9 hours ago

» Gacha Survey ( 1 2 )

Shizuna - Apr 11

59 by Kenrin »»
9 hours ago

» small form factor aka sff computer or pc

deg - Today

4 by deg »»
9 hours ago

» Are We Headed For Another Video Game Crash?

Retro8bit - Mar 15

22 by DesuMaiden »»
10 hours ago

» Portal

Dumb - Feb 26

16 by Fleeting_Dream »»
10 hours ago
It’s time to ditch the text file.
Keep track of your anime easily by creating your own list.
Sign Up Login