1

I am trying to output a string of charaters from one loop into an easygui.msgbox.

I tried running this code:

import easygui

n = 9

for i in range (9):
    if i == n:
        easygui.msgbox(str(i))
    else:
        easygui.msgbox(str(i)+",",)

But multiple windows open with one one cycle of the loop. And when i press okay the next number appears.

0, 

But my desired results are this in one mesagebox.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9
4

1 回答 1

1

每次调用easygui.msgbox,它都会打开一个消息框。
你在一个循环中调用它 9 次。
您只想调用一次,但使用完整的字符串

import easygui

msg = ','.join(str(i) for i in range(9))
easygui.msgbox(msg)

easygui.msgbox如果更容易理解,您可以像以前一样先单独构建字符串(但在构建完要显示的整个字符串之前 不要调用)。

  • 此外,由于range(9)从 0-8 开始,您if i == n:将不会做任何事情,因为 n==9。
  • 此外,如果您要n=9在开始时进行分配,您可能还想在循环中使用该变量for i in range(n):
于 2015-10-23T03:11:50.193 回答