Python Terminal issue

Alright, I’m learning python and wanted to practice and make a little dice rolling script, where you can define the size of the dice before you roll it. Using pycharm at the moment. In pycharms terminal, my time.sleep function to make the “Rolling . . .” more dramatic is working fine, but when I open it in Pythons Terminal, it doesn’t show up for the total specified time, then suddenly appears all at once.

from random import randint
import time
import os
diceCount = int()
userAnswer = int()

def Roll(diceCount):
diceRoll = randint(1, diceCount)
print(“Rolling.”, end=’’)
time.sleep(.5)
print(".", end=’’)
time.sleep(.5)
print(".", end=’’)
time.sleep(.5)
print(“You got “, diceRoll, “!!!”, sep=””)
time.sleep(1)
RollQuestion(diceCount)
def MakeNewDie():
diceCount = int(input("Please Input Your Die Number: "))
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
Roll(diceCount)

def RollQuestion(diceCount):
userAnswer = int(input("Would you rather roll again with this die or make a new one?\n1- Roll again!\n2- Make a new die!\ninput: "))
Answer(userAnswer, diceCount)

def Answer(userAnswer, diceCount):
if userAnswer == 1:
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
Roll(diceCount)
elif userAnswer == 2:
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
MakeNewDie()
print(“Welcome!”)
MakeNewDie()

1 Like

Alright, I’m learning python and wanted to practice and make a little dice rolling script, where you can define the size of the dice before you roll it.

It’s a “die”. A “die”!!! “Dice” is the plural. Just one of my bugbears.

Using pycharm at the moment. In pycharms terminal, my time.sleep
function to make the “Rolling . . .” more dramatic is working fine, but
when I open it in Pythons Terminal, it doesn’t show up for the total
specified time, then suddenly appears all at once.

Include flush=True in your print() calls, which causes the underlying
output stream to pass its data to the OS immediately. In PyCharm the
underlying stream is apparently unbuffered, but on a terminal the stdout
stream is normally line buffered, and you’re not writing complete lines
(ending in a newline character) - so you need to flush the output in
order to see it immediately. Eg:

print("Rolling.", end='', flush=True)

I wrote something bout buffering here:

https://www.mail-archive.com/python-list@python.org/msg450873.html

if you’re new to buffering. Apologies for the typos.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

I don’t know why it works in PyCharm, but the standard Python
environment buffers output. Output is only displayed when the buffer is
full, or when you print a newline, whichever happens first. Try this:

import time
for i in range(10):
    print(i, end=' ')
    for j in range(10):
        print('.', end='')
        time.sleep(0.2)
    print()

You should see the lines:

0: ..........

etc appear fully formed every two seconds.

Change the print lines like this:

for i in range(10):
    print(i, end=' ', flush=True)
    for j in range(10):
        print('.', end='', flush=True)
        time.sleep(0.2)
    print()

and you will get the effect you expected.

Thank you guys!!