Reputation: 11
I'm making a Python 3 program where it tells you to enter a rational number, converts that number into a floating point, passes it through a while loop where it actually iterates, then notifies the user if its bound or unbound (basically seeing if the program fits in the Mandelbrot set). My issue resides in the last part. I'm trying to make it check if the value run through the while loop is greater than than the original value, and if it is it'll print it's unbound. The same will happen vice versa. (I'm trying to do it all in an if/else) How do I refer to that original value though?
print('The best Mandelbrot iteration from Python!')
print('Please enter any rational you would like to check if it belongs to the Mandelbrot set.')
z = 0
c = input()
cc = c.replace(',', '.')
ccc = float(cc)
n = 0
while z < 1000 and n < 15 :
print( str(z) + (', ') + ( 'Count: ' + str(n) ) )
z = z**2 + ccc
n = n + 1
I've tried to make it print bound/unbound based on how many times it iterates. The program originally lasted forever if it was unbound. What I mean is this (n is the counter) :
if n >= 3:
print('Unbound!')
elif n < 3:
print('Bound!')
Upvotes: 1
Views: 57
Reputation: 185
If you just want to change the print at the end you could use f-strings.
if n >= 3:
print(f'{c} is unbound!')
elif n < 3:
print(f'{c} is bound!')
A more concise approach to get from getting the input into variable c
and having the float in ccc
would be ccc = float((c:=input()).replace(",","."))
.
I'm having trouble with your code not working correctly. I've found a small function on this site that works and returns a boolean result for the value being stable and thus in the Mandelbrot set.
def is_stable(c, num_iterations):
z = 0
for _ in range(num_iterations):
z = z ** 2 + c
return abs(z) <= 2
In your code you could this like so:
print(f{c} is {bound if is_stable(c,15) else unbound}.)
Hopefully this will help you.
Upvotes: 0