Print with python counter 3

Asked

Viewed 126 times

-1

Good afternoon guys, I’m trying to create an RPG of text in python, so to facilitate the writing of each episode of the game, I decided to create a statement of a text:

def episódios():
episódio = '{:^50}'.format(f'EPISÓDIO **{ESSE AQUI}**')
linha = '=' * 50
for l in linha:
    sys.stdout.write(l)
    sys.stdout.flush()
    time.sleep(0.02)
print('\n')
for l in episódio:
    sys.stdout.write(l)
    sys.stdout.flush()
    time.sleep(0.07)
print('\n')
for l in linha:
    sys.stdout.write(l)
    sys.stdout.flush()
    time.sleep(0.01)
print('\n')

I wonder if I could create a counter inside the keys I wrote "THIS HERE", so that each time I called the statement it would increase in ascending order from 1 in 1.

Example: I called it the first time: episodes() he writes on the screen 'Episode 1' and the next call 'Episode 2' and so on

Thanks in advance

1 answer

0

All you need to do is create an auxiliary variable to count and increment it within your function, so that the count increases with each call. See the example below:

contador = 0

def obter_episodio():
    global contador

    contador += 1
    return f"EPISÓDIO {contador}"

print(obter_episodio()) # EPISÓDIO 1
print(obter_episodio()) # EPISÓDIO 2
print(obter_episodio()) # EPISÓDIO 3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.