python 打印一段时间后完成

发布于 2024-11-02 22:59:55 字数 659 浏览 1 评论 0原文

我是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 技术交流群。

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

发布评论

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

评论(4

她说她爱他 2024-11-09 22:59:55

REPL 中的第一级块必须以完全空行结束。

>>> i=0
>>> while i < 10:
...   print i
...   i=i+1
... 
0
1
2
3
4
5
6
7
8
9
>>> print "done"
done

First-level blocks in the REPL must be terminated by a completely empty line.

>>> i=0
>>> while i < 10:
...   print i
...   i=i+1
... 
0
1
2
3
4
5
6
7
8
9
>>> print "done"
done
温馨耳语 2024-11-09 22:59:55

只需在 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.

巡山小妖精 2024-11-09 22:59:55

您可以使用 while..else控制结构。代码将是:

>>> i = 1
>>> while i < 10:
...     i = i + 1
... else:
...     print 'done'
...
...
done
>>>

尽管这通常用 python 编写为:

>>> for i in range(10):
...     pass
... else:
...     print 'done'
...
...
done
>>>

You can do this with the while..else control structure. The code would then be:

>>> i = 1
>>> while i < 10:
...     i = i + 1
... else:
...     print 'done'
...
...
done
>>>

Though this would typically be written in python as:

>>> for i in range(10):
...     pass
... else:
...     print 'done'
...
...
done
>>>
一片旧的回忆 2024-11-09 22:59:55

如果您看到“>>>”,则您没有编写程序。您正在使用口译员。你一次给它一个语句。

如果要编写程序,请将其保存在扩展名为 .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.

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