科拉茨猜想和打印陈述
我正在尝试创建一个简单的程序,将 Collatz 猜想的语句应用于用户可以输入的整数,我有:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Python 区分大小写。使用“打印”而不是“打印”。
Python is case sensitive. Use "print" not "Print".
好吧,你的语法错误是 python 区分大小写,所以你需要
print
而不是Print
。但您还有更多问题:
'n'
打印字符串n
。我认为您想要的是n
打印变量的值(或者如果不是,那么您可以只创建一个字符串“... true for n”)。最后(我认为),为了运行函数
collatz
,您不需要def
;这只是定义。Well, your syntax error is that python is case-sensitive, so you need
print
rather thanPrint
.But you've got more problems:
'n'
prints the stringn
. I think what you want isn
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 thedef
; that's just for the definition.更多问题:
n == 1
,而不是n == 0
。More problems:
n == 1
, notn == 0
.