For loop print statement problem

Joined
Jul 20, 2023
Messages
56
Reaction score
2
Why is it that when i write this code..
for i in range(5):
print('*', end = ' ')
time.sleep(.5)

it works in the pycharm console. That is it sequentially prints a * then sleeps for a half a second and prints another star directly after the last star and repeats 5 times.
Thus giving the impression of the computer is loading or computing or whatever.

But when I save the file and run it in a terminal it doesnt work like that. instead it acts more like this:
time.sleep(2.5)
print('*****')

it sleeps for the duration of the loop then prints the five stars all at once. What gives?
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
a slightly different approach
Python:
import sys
from time import sleep

for i in range(5):
    sys.stdout.write('*')
    sys.stdout.flush()
    sleep(.5)
Python:
import sys
from time import sleep

for _ in range(5):
    print('*', end='')
    sys.stdout.flush()
    sleep(.5)
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
and finally in python you can do it even like that ...
... some times syntax in python is poquito loco :cool:
Python:
from time import sleep

_ = [print('*', end='', flush=True) or sleep(.5) for _ in range(5)]
 
Joined
Dec 10, 2022
Messages
73
Reaction score
22
Playing around a bit

Python:
from time import sleep

string = 'This is a string'
def stars(arg):   
    return [print('*', end='', flush=True) or sleep(.15) for _ in range(arg)]


stars(len(string))
print(f'\n{string}')
stars(len(string))
print('\n')
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Playing around a bit

Python:
from time import sleep

string = 'This is a string'
def stars(arg):
return [print('*', end='', flush=True) or sleep(.15) for _ in range(arg)]


stars(len(string))
print(f'\n{string}')
stars(len(string))
print('\n')
nice one 👍...

but, like I said ...
syntax in python is poquito loco :cool:

Python:
from time import sleep

string = 'This is a string'
def stars(arg):  
    return [print('*', end='', flush=True) or sleep(.15) for _ in range(len(arg))]


[(lambda s: (stars(s), print(f'\n{s}'), stars(s)))(string)]
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
and this one...
completely illegible, don't write code like that, shown only for demonstration purposes


Python:
from time import sleep as p
s='This is a string'
[(print('*',end='',flush=True) or p(.15))for _ in s],print('\n'+s),[(print('*',end='',flush=True) or p(.15))for _ in s]
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
and finally in python you can do it even like that ...
... some times syntax in python is poquito loco :cool:
Python:
from time import sleep

_ = [print('*', end='', flush=True) or sleep(.5) for _ in range(5)]
Geez if you don't say. Mind boggled
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Wow some strange stuff indeed. So what exactly is this type of called?

Python:
_ = [print('*', end='', flush=True) or sleep(.5) for _ in range(5)]

An expression or comprehension or something. Or just weird? lol
 
Joined
Dec 10, 2022
Messages
73
Reaction score
22
List comprehension is a short way to put a for loop on one line.
As in most languages the or is saying either print this or print that.
It just running the sleep command.
or can be used in inputs as well as a default value. For example

Python:
var = input('Type something: ') or 'Print default value'
print(var)

If you type something and press enter, what's typed will print.
If you just press enter without typing anything the default value will display.
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
List comprehension is a short way to put a for loop on one line.
As in most languages the or is saying either print this or print that.
It just running the sleep command.
or can be used in inputs as well as a default value. For example

Python:
var = input('Type something: ') or 'Print default value'
print(var)

If you type something and press enter, what's typed will print.
If you just press enter without typing anything the default value will display.
oh so if the value is false it will use the other? But in our case its actually using both right? And the sleep statement actually becomes a value inside the list?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top