python 打印一段时间后完成
我是Python新手: 我的目标是在 while 循环后打印完成语句 但它给了我语法错误
>>> i=0
>>> while i < 10:
... print i
... i=i+1
...
... print "done"
File "<stdin>", line 6
print "done"
^
SyntaxError: invalid syntax
<?php
$i=0;
while($i<10)
{
echo "$i \n";
}
echo "done";
?>
我正在尝试在 python 中复制相同的 php 程序,
但
>>> i=0
>>> while i < 10:
... print i
... i=i+1
... print "done"
File "<stdin>", line 4
print "done"
^
SyntaxError: invalid syntax
仍然失败 我们不能在结束后使用打印还是我们必须等待一段时间才能完成并进行打印
I am new to python:
my aim is to print a done statement after while loop
but it gives me syntax error
>>> i=0
>>> while i < 10:
... print i
... i=i+1
...
... print "done"
File "<stdin>", line 6
print "done"
^
SyntaxError: invalid syntax
<?php
$i=0;
while($i<10)
{
echo "$i \n";
}
echo "done";
?>
I am trying to replicate the same php program in python
i tried
>>> i=0
>>> while i < 10:
... print i
... i=i+1
... print "done"
File "<stdin>", line 4
print "done"
^
SyntaxError: invalid syntax
still it fails
cant we use a print after end or do we have to wait for the while to finish and do the print
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
REPL 中的第一级块必须以完全空行结束。
First-level blocks in the REPL must be terminated by a completely empty line.
只需在 while 循环之后删除空行上的空格即可。空格使解释器认为循环仍在继续。
Just get rid of that space on your empty line after the while loop. The space makes the interpreter think that the loop is continuing.
您可以使用
while..else控制结构
。代码将是:
尽管这通常用 python 编写为:
You can do this with the
while..else
control structure. The code would then be:Though this would typically be written in python as:
如果您看到“>>>”,则您没有编写程序。您正在使用口译员。你一次给它一个语句。
如果要编写程序,请将其保存在扩展名为 .py 的纯文本文件中。您应该能够通过双击它来运行它(尽管它不会在最后暂停,因此您可能只会看到命令窗口闪烁),或者通过将文件名作为参数提供给 python 在命令行中。
If you see '>>>', you are not writing a program. You are using an interpreter. You feed it one statement at a time.
If you want to write a program, save it in a plain text file with a .py extension. You should be able to run this by double-clicking it (although it will not pause at the end, so you might just see the command window flash), or by supplying the file's name as an argument to
python
at the command line.