Skip to content
Sign upLog in
← Back to Community

Add-ons to your Python code to make it better!

Profile icon
Bookie0

Hello hello hello wassup people,

I’m just going to show you some quick things to make your Python code cool!

1) Screen Clearing

Screen clearing is very useful; you use it to, well, clear the screen. I like it a lot because you can use it to erase everything so that it looks better, and so that not too much text crowds the screen!

For the screen clearing, you need to just:

os.system('clear')

You can copy this code and put it every time you need to.
However, i prefer to create a function, so that i dont need to keep putting that (even if its just a few letters...):

def sc(): os.system(‘clear’)

As you can see I called the function sc(),which stands for screen clear, so each time I want to clear the screen, i just call that function. It makes it just a bit shorter.
(Thanks to

@ArjunSS1
for helping me with the screen clear)

2) Typewriter effect

Now this typewriter effect is super cool, because it makes the text print one letter at the time. You can regulate it so that it prints the text fast or slow.
First the imports:

import sys, os, curses import time

And this is the code:

st = 0.04 def sp(str): for letter in str: sys.stdout.write(letter) sys.stdout.flush() time.sleep(st) print()

I usually put it at 0.04because it prints it not too fast and not too slow. But you can change it. Just change the value after st

Each time you want to do this typewriter effect, just do:

sp(“Hello World”)

Output:

Hello World

3) While loops to go back to menu (very basic)

Personally, I’m not a pro in loops, but this is what I do sometimes. Imagine you want to do sort of menu option, like this:

print (“This is the menu”) action1 = input(“Type hello for some greetings > ”) if action1 == “hello”: print(“Hola”) print(“Bonjour”) print(“Aloha”)

So when the user types hello, several greetings will appear. But what if the user types something else by accident? Then, you could add an else statement, and saying something like “invalid”, and then the user could return to the menu,
This is how you do that:

back_to_menu = True while back_to_menu == True: print (“This is the menu”) action1 = input(“Type “hello” for some greetings > ”) if action1 == “hello”: print (“Hola”) print (“Bonjour”) print (“Aloha”) else: print (“You wrote something else than “hello”! ”) back_to_menu = True

So when you put back_to_menu = True at the end of the else statement, the user will arrive back at the menu!

4) Colors

This is how to make colors in Python.

Pretty simple, just copy paste these colors (because typing them would be to long...)

# colors black = "\033[0;30m" red = "\033[0;31m" green = "\033[0;32m" yellow = "\033[0;33m" blue = "\033[0;34m" magenta = "\033[0;35m" cyan = "\033[0;36m" white = "\033[0;37m" bright_black = "\033[0;90m" bright_red = "\033[0;91m" bright_green = "\033[0;92m" bright_yellow = "\033[0;93m" bright_blue = "\033[0;94m" bright_magenta = "\033[0;95m" bright_cyan = "\033[0;96m" bright_white = "\033[0;97m"

So to use it, just put the color before the text:

print(bright_yellow + “text here”)

This also works with the slowPrintI mentioned earlier

5) Some functions you can use

I always use these functions in my code because they are kinda like shortcuts. Less is more!

# for putting some space in between stuff def space(): print() print() # prompt the user to press enter def enter(): input("Enter to continue\n> ") # btw: \n is for another line

So that’s about it i know this tutorial isn’t very good, but I hope it helped you and you enjoyed it.
Lastly, nothing is under copyright, so go on and copy paste!

Tips, suggestions, questions, comments to let me know stuff, etc. Feel free to contact me!

Voters
Profile icon
aolifdood
Profile icon
DariusCC
Profile icon
ogund2887
Profile icon
Yoplayer1py
Profile icon
AzureWarrior
Profile icon
johnnyfrancis
Profile icon
SilvermoonCat
Profile icon
IntellectualGuy
Profile icon
DominicHuieWhit
Profile icon
programmeruser
Comments
hotnewtop
Profile icon
ArjunSS1

Small things, good article but you could do these things

You can use os to clear the screen instead like os.system('clear') which clears the screen
2.
For the typewriter there is an inbuilt argument in your print statement so instead of running sys.stdout.flush() you can just print(char, end='', flush=True)

Intead of running whille_back_to_menu = True you can just run while back_to_menu which is true so it will loop alternatively you can run while 1: which is True and you add a break statement to the if action1 == 'hello' block.

Profile icon
NoelB33

Also you can print a certain string to clear the screen also.

@ArjunSS1

Profile icon
Bookie0

Hey, thanks for commenting! I’m gonna change the screen clear and out your name for credit

@ArjunSS1

Also for the typewriter I just put a function so that it’s shorter
For the back to menu, I know it can always loop back, but sometimes the user wants to just go to another thing instead of looping back.

But anyways thanks for the comments and remember,

Profile icon
Leroy01010

This helped with my python exam

Profile icon
Bookie0

Cool, glad it helped! :)

@Leroy01010

Profile icon
johnnyfrancis

Finally! Now to make colorful code!

Profile icon
Bookie0

Lol have fun! :)

@johnnyfrancis

Profile icon
TheJuiceTin

This helped me a LOT

Profile icon
Bookie0

Great thanks!

@TheJuiceTin

Profile icon
[deleted]

I used like four of the five things given in this tutorial for my python project. Thx for this

@Bookie0
!

Profile icon
Bookie0

@GhostKing007
no problem, glad it helped! :)

Profile icon
TROD2007

@Bookie0
hey, this helped me a lot, and maybe you could add the thingy you put on my repl for rock, paper, scissors for colors like the
from colored import fg, stylize
thingy

Profile icon
Bookie0

@TROD2007
glad this helped! But I think ANSI escape codes are a bit easier, and more of them! :)

Profile icon
TROD2007

@Bookie0
ok, thx, ill see if I can use them

Profile icon
Bookie0

nice!! :)

@TROD2007

Profile icon
TROD2007

@Bookie0
hey, so i have a question. When you put something, like a print statement, how do you make it so that way when you click 'enter', it takes you to a new screen where it continues the code? Can you help me?

Profile icon
Bookie0

@TROD2007
well you can make a function with a menu screen, then some conditionals to check when the user presses enter

Profile icon
TROD2007

@Bookie0
ok, do you want to help make some stories? and some help on other stuffs?

Profile icon
TROD2007

@Bookie0
hi i invited you to the TOPPER SECRET repl. could you please help me?

Profile icon
TROD2007

@Bookie0
hi, can you help me with something pls?

Profile icon
TROD2007

@Bookie0
it's on the top secret repl. Pls?

Profile icon
Bookie0

yez?

@TROD2007

Profile icon
TROD2007

@Bookie0
can you come to the repl?

Profile icon
Bookie0

link

@TROD2007

Profile icon
TROD2007

@Bookie0
didn't i invite you?

Profile icon
Bookie0

can u pls just gib link it would be easier thx

@TROD2007

Profile icon
TROD2007

@Bookie0
ok sry

Profile icon
TROD2007

@Bookie0
here you go
https://repl.it/@TROD2007/TOP-SECRET-DONT-ENTER

Profile icon
TROD2007

@Bookie0
hi, i sent you a discord invite and also i need help

Profile icon
TROD2007

@Bookie0
here link
https://repl.it/@TROD2007/TOP-SECRET-DONT-ENTER

Profile icon
Bookie0

im on it

@TROD2007

Profile icon
TROD2007

@Bookie0
hi, sry, I couldn't tell you on discord, but i need help pls

Profile icon
Bookie0

tomorrow. ping me with repl link and what isn't working :)

@TROD2007

Profile icon
TROD2007

@Bookie0
okay tokay

Profile icon
TROD2007

@Bookie0
hey Bookie, i need help on the top secret thingy. I'm trying to make it into one of those continue things where it keeps asking them the same question until they give an answer that they are supposed to do. or can you help me with the turtle thing. cuz I think that if I use it, I will be able to make buttons, right?

Profile icon
TROD2007

@Bookie0
also here is the URL
https://repl.it/@TROD2007/TOP-SECRET-DONT-ENTER

Profile icon
Bookie0

aight, I'll try to help you. Tomorrow ping me w/ link to repl

also idk much turtle python but I'll still try to help! :D

@TROD2007

Profile icon
TROD2007

@Bookie0
ok, well at the moment, i just need to figure out how to make it run, and later we can use the turtle function to make it into a real game

Profile icon
TROD2007

@Bookie0
thx tho, baiiii

Profile icon
TROD2007

@Bookie0
hey Bookie, im taking a little break from the top secret repl, now I made a new one and it is the game M.A.S.H do you know what it is? I need some help I think I can do something but im not sure. here the URl
https://repl.it/@TROD2007/MASH#main.py

Profile icon
Imthebestthe

would there be a way t make the color a def function so you can just do like green('test') for example?

Profile icon
Bookie0

@Imthebestthe
hum yes, but how would you do slowprint? or input?

Profile icon
PYer

Instead of doing while back_to_menu == True just do while back_to_menu. Also, instead of defining a hundred variables, why not make it into a dictionary. So instead of cyan do cls[´cyan´] it makes it more organized in my opinion. Also, with this system, it is easy to implement a function to change the color of an entire print statement:

def pc(t, c): print(cls[c] + t + cls[´white´])
Profile icon
Bookie0

Ah ok then, thanks for the suggestions. For the while loop thing, I had made this like a while ago, so I didn’t know about what you suggested.

And for the colors, I had no idea how to do it; also I made it into variables so that it seemed a bit more easier, especially for beginners ;)
But thanks anyways

@PYer

Profile icon
PYer

ahh okay.

@Bookie0

Profile icon
Bookie0

ok :)

@PYer

Profile icon
Leroy01010

thanks

Profile icon
Bookie0

np ;)

@Leroy01010

Profile icon
Leroy01010

do you want to be in our game as a character

@Bookie0

Profile icon
Bookie0

what game? also sure you can make me as a character ;)

@Leroy01010

Profile icon
Leroy01010

ok

@Bookie0

Profile icon
Leroy01010

we have an error can you please fix it

@Bookie0

Profile icon
Bookie0

ok repl link and i will join ;)

@Leroy01010

Profile icon
Leroy01010

https://repl.it/join/lumizpoi-leroy01010

@Bookie0

Profile icon
gibbsfreenergy

Ummm

@Bookie0
how do i add the typewriter effect thing for inputs.
Such as how do I add it to this: "email=input("What is the email address:").strip()"
Where do I put 'sp1' in the input?

Profile icon
Bookie0

yea i dont think you can add the typewriter effect to inputs...

@JosephSanthosh

Profile icon
gibbsfreenergy
Profile icon
Bookie0