I have used the following code:
import math
import decimal
def calculate_c(a, h):
return (1 * 15 * (4 * (h ** (3/2))) ) / (3 * (a ** (1/2)))
def calculate_d(a, h, u):
return 2 * ((u * h) - ((1/3) * a * (u ** 3)))
def calculate_F(a, u):
return ((math.arcsinh(2 * abs(a) * u) / (4 * abs(a))) + ((u * np.sqrt((4 * (a ** 2) * (u ** 2)) + 1)) /2))
def calculate_g(F):
return 2 * F * 1.24 * 0.5 * 0.2 * 15
# Definiëren van zoekbereik
def range(x, y, jump):
while x < y:
yield float(x)
x += decimal.Decimal(jump)
h_values = list(range(0, 150, '0.1'))[-1] # h moet kleiner zijn dan 15
a_values = range(0, 100, '0.1') # a mag niet te klein zijn om te voorkomen dat sqrt(h/a) < 7.5
max_c = 0
best_h, best_a = None, None
for h in h_values:
for a in a_values:
u = math.sqrt(h / a)
if u < 7.5:
d = calculate_d(a, h, u)
if d <= 25:
c = calculate_c(a, h)
if c > max_c:
max_c = c
best_h, best_a = h, a
best_u = math.sqrt(best_h / best_a)
best_d = calculate_d(best_a, best_h, best_u)
best_F = calculate_F(best_a, best_u)
best_g = calculate_g(best_F)
print(f'Maximale waarde van c: {max_c}')
print(f'Optimale waarde van h: {best_h}')
print(f'Optimale waarde van a: {best_a}')
print(f'Waarde van u: {best_u}')
print(f'Waarde van d (dwarsdoorsnede): {best_d}')
print(f'Waarde van F: {best_F}')
print(f'Gewicht van de boot (g): {best_g}')
Final prints and explanations with # are in Dutch, this is not important. When I try to run it, I get the following error:
Traceback (most recent call last):
File "", line 25, in
TypeError: 'float' object is not iterable
It says it cannot iterate float, but I also tried printing it and it only returned one value. The lines for generating the lists came from this site, but I am not very good at coding and struggle to integrate it into my function. Is there any way to resolve this?
I need the values to be floats.
I have used the following code:
import math
import decimal
def calculate_c(a, h):
return (1 * 15 * (4 * (h ** (3/2))) ) / (3 * (a ** (1/2)))
def calculate_d(a, h, u):
return 2 * ((u * h) - ((1/3) * a * (u ** 3)))
def calculate_F(a, u):
return ((math.arcsinh(2 * abs(a) * u) / (4 * abs(a))) + ((u * np.sqrt((4 * (a ** 2) * (u ** 2)) + 1)) /2))
def calculate_g(F):
return 2 * F * 1.24 * 0.5 * 0.2 * 15
# Definiëren van zoekbereik
def range(x, y, jump):
while x < y:
yield float(x)
x += decimal.Decimal(jump)
h_values = list(range(0, 150, '0.1'))[-1] # h moet kleiner zijn dan 15
a_values = range(0, 100, '0.1') # a mag niet te klein zijn om te voorkomen dat sqrt(h/a) < 7.5
max_c = 0
best_h, best_a = None, None
for h in h_values:
for a in a_values:
u = math.sqrt(h / a)
if u < 7.5:
d = calculate_d(a, h, u)
if d <= 25:
c = calculate_c(a, h)
if c > max_c:
max_c = c
best_h, best_a = h, a
best_u = math.sqrt(best_h / best_a)
best_d = calculate_d(best_a, best_h, best_u)
best_F = calculate_F(best_a, best_u)
best_g = calculate_g(best_F)
print(f'Maximale waarde van c: {max_c}')
print(f'Optimale waarde van h: {best_h}')
print(f'Optimale waarde van a: {best_a}')
print(f'Waarde van u: {best_u}')
print(f'Waarde van d (dwarsdoorsnede): {best_d}')
print(f'Waarde van F: {best_F}')
print(f'Gewicht van de boot (g): {best_g}')
Final prints and explanations with # are in Dutch, this is not important. When I try to run it, I get the following error:
Traceback (most recent call last):
File "", line 25, in
TypeError: 'float' object is not iterable
It says it cannot iterate float, but I also tried printing it and it only returned one value. The lines for generating the lists came from this site, but I am not very good at coding and struggle to integrate it into my function. Is there any way to resolve this?
I need the values to be floats.
Share Improve this question edited yesterday simon 5,1331 gold badge15 silver badges28 bronze badges asked Feb 7 at 14:06 user29547777user29547777 11 bronze badge New contributor user29547777 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 7 | Show 2 more comments1 Answer
Reset to default 0This is what your line means:
h_values = list(range(0, 150, '0.1'))[-1]
You're constructing a range of values from 0 to 150 with a step of 0.1. (Here you've redefined the range
keyword to be a generator, which is not a good idea, as it will confuse your readers and make the range
feature unavailable to you. Python has a bit of a misfeature in that you can redefine keywords to do something else and it won't warn you.) Anyway, in this line, you're generating all the values from 0 to 149.9 (stepped by 0.1), converting the generated values into a list (which is inefficient), then indexing the list ([]
) from the end (-
) by one, i.e., taking the last member. So this whole expression evaluates to the end of the range, 149.9
(a rather convoluted way to get the value!). This is a single float
, which as you discovered you cannot use in a for
statement, because the for
statement requires something that can iterate, that is, generate multiple values (like a list). This is not what you want anyway. The key problem is the [-1]
; it's not clear what you intended to do here, but I expect you'll want to remove it, although as someone else pointed out, you'll get another problem after that, namely, a divide-by-zero error, because the first value of h
and the first value of a
will be zero and in line 27 you try to divide them. I don't know what you are trying to do, but maybe changing the range
to start at 0.1 might be better.
Also your a_values
will exhaust after one iteration of the outer loop; e.g. if we did this:
h_values = range(0, 10, '0.1')
a_values = range(decimal.Decimal('0.1', 0.5, '0.1')
for h in h_values:
for a in a_values:
print(f"h={h} a={a}")
the output would be
h=0.0 a=0.1
h=0.0 a=0.2
h=0.0 a=0.3
h=0.0 a=0.4
which is not what you want. This is because generator instances, after stopping, do not renew themselves but remain stopped. You need to create a new instance of the a_values
generator within the loop, like this:
h_values = frange(0, 10, '0.1')
for h in h_values:
a_values = frange(decimal.Decimal('0.1'), 0.5, '0.1')
for a in a_values:
print(f"h={h} a={a}")
You said the Dutch was irrelevant, but your comment in the h_values
assignment says "h must be less than 15" but you're giving it a value of up to 149.9.
Also you have a reference to np.sqrt
and you said something about not being able to import numpy
. I am not sure what that's about.
So, putting it all together, if I've understood you correctly:
import decimal
import math
def calculate_c(a, h):
return (1 * 15 * (4 * (h ** (3/2))) ) / (3 * (a ** (1/2)))
def calculate_d(a, h, u):
return 2 * ((u * h) - ((1/3) * a * (u ** 3)))
def calculate_F(a, u):
return ((math.asinh(2 * abs(a) * u) / (4 * abs(a))) + ((u * math.sqrt((4 * (a ** 2) * (u ** 2)) + 1)) /2))
def calculate_g(F):
return 2 * F * 1.24 * 0.5 * 0.2 * 15
# Definiëren van zoekbereik
def frange(x, y, jump):
while x < y:
yield float(x)
x += decimal.Decimal(jump)
h_values = frange(0, 15, '0.1') # h moet kleiner zijn dan 15
max_c = 0
best_h, best_a = None, None
for h in h_values:
a_values = frange(decimal.Decimal('0.1'), 100, '0.1') # a mag niet te klein zijn om te voorkomen dat sqrt(h/a) < 7.5
for a in a_values:
u = math.sqrt(h / a)
if u < 7.5:
d = calculate_d(a, h, u)
if d <= 25:
c = calculate_c(a, h)
if c > max_c:
max_c = c
best_h, best_a = h, a
best_u = math.sqrt(best_h / best_a)
best_d = calculate_d(best_a, best_h, best_u)
best_F = calculate_F(best_a, best_u)
best_g = calculate_g(best_F)
print(f'Maximale waarde van c: {max_c}')
print(f'Optimale waarde van h: {best_h}')
print(f'Optimale waarde van a: {best_a}')
print(f'Waarde van u: {best_u}')
print(f'Waarde van d (dwarsdoorsnede): {best_d}')
print(f'Waarde van F: {best_F}')
print(f'Gewicht van de boot (g): {best_g}')
This yields:
Maximale waarde van c: 375.00000000000006
Optimale waarde van h: 7.5
Optimale waarde van a: 1.2
Waarde van u: 2.5
Waarde van d (dwarsdoorsnede): 25.0
Waarde van F: 8.122573965507131
Gewicht van de boot (g): 30.21597515168653
If you need to do "decimal float" arithmetic using Decimal
, let me know and I'll try to adapt this solution.
[-1]
when initialisingh_values
? That converts your list to a float, which is no longer iterable. – Macavity Commented Feb 7 at 14:27h_values
is a single float value of 149.9 because you select the last element of the List by using [-1]. Iterating over this is then not meaningful. Note that with this issue resolved you are dividing by elements from Lists which have Zero values hence will get a divide by Zero error, – user19077881 Commented Feb 7 at 15:38range(0, 150, '0.1')
is not valid. All the arguments torange()
must be integers, not floats. See stackoverflow.com/questions/7267226/range-for-floats for how to do float ranges. – Barmar Commented Feb 7 at 16:15