科拉茨猜想和打印陈述

发布于 2024-12-12 21:43:15 字数 447 浏览 0 评论 0原文

我正在尝试创建一个简单的程序,将 Collat​​z 猜想的语句应用于用户可以输入的整数,我有:

def collatz(n):
    print n,
    if n % 2 ==0:
        n = n / 2
    elif n == 0:
        Print "Collatz Conjecture true for" , 'n'
    else:
        n = n *3 + 1

input("\n\nInsert a positive integer:")
def collatz(n)

但是它说该行存在语法错误:

Print "Collatz Conjecture true for" , 'n'

我看不出有什么错误在这一行。

另外,由于我还无法测试它,这看起来可以正常工作吗?

I am trying to create a simple program to apply the statement of the Collatz Conjecture to an integer that the user can enter, I have:

def collatz(n):
    print n,
    if n % 2 ==0:
        n = n / 2
    elif n == 0:
        Print "Collatz Conjecture true for" , 'n'
    else:
        n = n *3 + 1

input("\n\nInsert a positive integer:")
def collatz(n)

However it is saying there is a syntax error in the line:

Print "Collatz Conjecture true for" , 'n'

I can't see what mistake ther is in this line.

Also as I haven't been able to test it yet, does this look as though it will work ok?

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

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

发布评论

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

评论(4

瀟灑尐姊 2024-12-19 21:43:15

Python 区分大小写。使用“打印”而不是“打印”。

Python is case sensitive. Use "print" not "Print".

拒绝两难 2024-12-19 21:43:15

好吧,你的语法错误是 python 区分大小写,所以你需要 print 而不是 Print

但您还有更多问题:

  • 'n' 打印字符串n。我认为您想要的是 n 打印变量的值(或者如果不是,那么您可以只创建一个字符串“... true for n”)。

  • 最后(我认为),为了运行函数 collat​​z,您不需要 def;这只是定义。

Well, your syntax error is that python is case-sensitive, so you need print rather than Print.

But you've got more problems:

  • 'n' prints the string n. I think what you want is n to print the value of the variable (or if not, then you can just make a single string "... true for n").

  • Finally (I think), in order to run the function collatz, you don't need the def; that's just for the definition.

分開簡單 2024-12-19 21:43:15

更多问题:

  1. 停止条件应该是n == 1,而不是n == 0
  2. 你必须重复或迭代,因为你只迈出了一步。
  3. 检查输入,确保它确实是正数。

More problems:

  1. The stopping condition should be n == 1, not n == 0.
  2. You have to recur or iterate, as is you're only making one step.
  3. Check the input, make sure it really is a positive number.
请远离我 2024-12-19 21:43:15
 def collatz_steps(n):
    steps=0
    if n==1:
        return 0 
    else:
        while n!=1:
            if n%2==0:
                n=n/2
                steps+=1
            else:
                n = 3*n+1
                steps+=1
        return steps
 def collatz_steps(n):
    steps=0
    if n==1:
        return 0 
    else:
        while n!=1:
            if n%2==0:
                n=n/2
                steps+=1
            else:
                n = 3*n+1
                steps+=1
        return steps
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文