简体   繁体   中英

How to output an entire loop in easygui python

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

Every time you call easygui.msgbox, it will open a message box.
You're calling it 9 times in a loop.
You want to just call it once, but with your complete string

import easygui

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

if it's easier to understand, you can build up your string separately first like you did (but don't call easygui.msgbox until you have built up the entire string you want to display).

  • Also, since range(9) goes from 0-8, your if i == n: won't do anything since n==9.
  • Also, if you're going to assign n=9 at the start, you probably want to use that variable in your loop as well for i in range(n):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM