在进行 Mandelbrot 迭代绑定/非绑定测试器时,您可以在 Python 3 中使用 while 循环后引用原始变量值吗?

发布于 2025-01-17 00:04:01 字数 747 浏览 1 评论 0原文

我正在制作一个 Python 3 程序,它告诉您输入一个有理数,将该数字转换为浮点数,将其传递给实际迭代的 while 循环,然后通知用户它是否已绑定或未绑定(基本上是查看是否该程序符合 Mandelbrot 集)。我的问题在于最后一部分。我试图让它检查 while 循环运行的值是否大于原始值,如果是,则会打印它是未绑定的。反之亦然也会发生同样的情况。 (我试图在 if/else 中完成这一切)但是我如何引用该原始值?

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

我试图根据它的迭代次数使其打印绑定/未绑定。原本,如果未绑定,该程序将永远持续下去。我的意思是这样的(n是计数器):

if n >= 3:
    print('Unbound!')
elif n < 3:
    print('Bound!')
    

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!')
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄想挽回 2025-01-24 00:04:01

如果您只想更改最后的打印,可以使用 f 字符串。

if n >= 3:
    print(f'{c} is unbound!')
elif n < 3:
    print(f'{c} is bound!')

将输入输入到变量 c 并将浮点数放入 ccc 中的更简洁方法是 ccc = float((c:=input()) .replace(",","."))

我遇到了您的代码无法正常工作的问题。我在这个 site 可以工作并返回一个布尔结果,因为该值是稳定的,因此在 Mandelbrot 集中。

def is_stable(c, num_iterations):
    z = 0
    for _ in range(num_iterations):
         z = z ** 2 + c
    return abs(z) <= 2

在你的代码中你可以这样:
print(f{c} is {bound if is_stable(c,15) else unbound}.)

希望这会对您有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文