我在Python进行循环时遇到问题

发布于 2025-02-10 20:02:46 字数 164 浏览 1 评论 0原文

因此,我正在制作一个需要选择一个数字的程序(例如10),然后添加一个循环,每次都会在我的号码中添加一个单元。我已经尝试过:

while true :
    num = 10
    num += 1
    print(num)

您能帮我吗?谢谢。

so I'm making a program that needs to pick a number (like 10) then add a loop that adds one unit to my number Everytime. I have tried this :

while true :
    num = 10
    num += 1
    print(num)

can you help me on this? thanks.

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

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

发布评论

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

评论(2

禾厶谷欠 2025-02-17 20:02:46

您的示例不起作用,因为您每次都将其重置为10。

您必须这样做:

num = 10 #Set it to 10 once
while True:
   num += 1 #add 1 each time
   print(num)

请注意,true是无效的,在Python中,您使用true。
另请注意,Python基于Whitespace,因此您将在if语句或循环中永不使用(bool)。这只是浪费空间添加()

Your example won’t work because you are resetting it to 10 every time.

You have to do so something like this:

num = 10 #Set it to 10 once
while True:
   num += 1 #add 1 each time
   print(num)

Note that true is invalid and in python you use True.
Also note that python is based on whitespace so you will Never use (bool) in a if statement or a loop. It is just a waste of space to add ()

长不大的小祸害 2025-02-17 20:02:46

尝试做

while (true):
    num = 10
    num += 1
    print(num)

Try doing

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