1
\$\begingroup\$

Is there any simple way to improve this revamped code from an earlier version? I am looking for improvements that are SIMPLE, not more that post-advanced please.

import time
import os, signal

#Username = '_'

A = '''

<!DOCTYPE html> is the declaration tag for a standard HTML
document. This is used to tell the browser what kind of HTML document
it is.

'''
a = '''

<p> This tag defines a PARAGRAPH. These are highly customisable
in CSS, and can be used in any HTML version. There are variations
of this tag, such as:

<p1>
<p2>
<p3>
<p4>
<p5>
<p6>

and so on..

'''
B = '''

<h> This tag defines a HEADER. These, like PARAGRAPHS (see <p>),
are highly custonizable in CSS, and, like PARAGRAPHS, can be used
in any HTML version. There are variations of this tag, such as:

<h1>
<h2>
<h3>
<h4>
<h5>
<h6>

and so on..

'''
b = '''

<a> This tag defines a HYPERLINK. This can be used to turn ANY block
or peice of text into a clickable link. This has a variety of uses, such as:

Redirection
Buttons
Drop down menus
Navigations bars

and many more.

A hyperlink's syntax is as shows:

<a href = "www.LINK.com"  target = "(_blank, _parent, _self, and _top are
your options)" type = "(specify the type of linked document.)"></a>

The closing tag is nessesary for this to work with your given parameters.

'''
C = '''

<div> This tag defines a DIVIDED BLOCK where you can store content,
such as pictures, videos, buttons, text, and just about anything you can
think of. This is mainly used for containing things such as:

Navigation bars
Buttons
The entire page (Formally known as "Wrapper")
ETC.

This DIV's syntax is as shows:

<div class = "(calls a certain CSS class)"></div>

The closing tag is nessesary for this to work with your given parameters.

'''
c = '''

<link> This tag will LINK (a) CSS file(s) to your HTML document. You can
LINK multiple CSS files to a single HTML document. You can LINK files
from Google, as well as your own .css files.

The LINK's syntax is as shows:

<link href = "FILENAME.css (OR) www.LINK.com" rel = "Stylesheet" type =
"text/css">

'''

print("This tool was made to help programmers, like myself,")
print("with HTML tags. It takes your input, say you typed")
print("<div>, it would give you an accurate description of")
print("that tag and a list of its uses.")
print("")
print("Guest users please login as 'Guest'")
print("")
print("This tool was made by Chase Barnes")
print("@Copyright 2017")
print("")
print("(Remember, type BASE tags -<p>- ONLY)")
print("")

while True:
    Username = input("Username: ")
    if Username == 'Guest':
        Username = 'Guest'
        break
    elif Username == '/admin061503':
        Username = "Admin Chase"
        Pass = input("Password: ")
        if Pass == 'Slenderman123':
            Username = 'Admin Chase'
            break
        else:
            print("")
            print("The Input is Incorrect")
            print("")
    else:
        print("")
        print("UNKNOWN USERNAME")
        print("")
        continue


print("")
print("Welcome, " + Username + "!")
print("")

def main():
    Input = input("TAG: ")
    if Input == '/exit':
        answerexit = input("Are you sure? ")
        if  answerexit == 'yes':
            print("")
            print("SHUTTING DOWN..")
            print("")
            time.sleep(2)
            os.kill(os.getpid(), signal.SIGTERM)
        elif answerexit == 'no':
            print("")
            print("SHUTDOWN ABORTED..")
            print("")
            main()
    elif Input == '':
        print("")
        print("Please enter a tag or command.")
        print("")
        main()
    elif Input == '<!DOCTYPE html>':
        print(A)
        time.sleep(1)
        main()
    elif Input == '<p>':
        print(a)
        time.sleep(1)
        main()
    elif Input == '<h>':
        print(B)
        time.sleep(1)
        main()
    elif Input == '<a>':
        print(b)
        time.sleep(1)
        main()
    elif Input == '<div>':
        print(C)
        time.sleep(1)
        main()
    else:
        print("")
        print('''The tag or command you entered is Not Recognized of is not yet
supported by this tool.''')
        print("")
        main()

main()

PURPOSE (readme.txt):

Hey, You! Hello there! This programs function is to give information about HTML tags and their uss/functions.

CHANGELOG:

Version_1 (9/15/2017):

  Login Feature added         Six tags are added:             <!DOCTYPE html>             <p>
      <a>             <h>             <link>          <div>       Startup info added      Shutdown

feature added (Does not shutdown your computer, just the file) added TWO users: -Admin Chase -Guest

Users:

  Username: Guest         Password:
          Username: /admin061503      Password: *************

   How To Use:

DO NOT open in pythons command prompt, open through IDLE or Python shell (IDLE)

Enter Username on startup, then password. (If set)

After you are logged in, Enter a tag or command!

List of all keywords:

<!DOCTYPE html>
<p>
<h>   <a>     <div>   <link>  /exit       yes         no
\$\endgroup\$
5
  • \$\begingroup\$ What indentation? \$\endgroup\$
    – EgMusic
    Sep 15, 2017 at 15:35
  • \$\begingroup\$ And, also, I have a README.txt file inside the folder of this py file. \$\endgroup\$
    – EgMusic
    Sep 15, 2017 at 15:36
  • \$\begingroup\$ I put the contents of the readme inside of the question \$\endgroup\$
    – EgMusic
    Sep 15, 2017 at 15:38
  • \$\begingroup\$ Why does every line start with four spaces? \$\endgroup\$
    – Peilonrayz
    Sep 15, 2017 at 15:44
  • \$\begingroup\$ because before I posted the question, I had to indent it that much for it to register as code. That's what it said to do. \$\endgroup\$
    – EgMusic
    Sep 15, 2017 at 15:45

2 Answers 2

7
\$\begingroup\$

Variable naming

The names of the variables defined at the top of your script are meaningless and do not convey their purpose.

Check for __main__

Enclose script code to be run within an

if __name__ == '__main__':
    <code_here>

block, so that it does not run if the module is solely imported.

PEP8

Read and implement PEP8.

Text files

You should consider putting the running "help" text into files (plain text, INI, JSON etc.) and reading them within your source code.

Termination

Terminating the program by

os.kill(os.getpid(), signal.SIGTERM)

seems cumbersome. Use exit or sys.exit instead.

\$\endgroup\$
2
  • \$\begingroup\$ I tried using sys.exit() and exit(), but It kept saying ("The program is still running, do you want to kill it?" and I want it to 'shutdown' like a computer does, without having to mess with that silly dialogue box, so that is why I used os.kill(os.getpid(), signal.SIGTERM) \$\endgroup\$
    – EgMusic
    Sep 16, 2017 at 17:28
  • \$\begingroup\$ @ChaseBarnes This seems like a separate issue you may consider discussing on stackoverflow. stackoverflow.com/questions/38028151/… \$\endgroup\$ Sep 18, 2017 at 10:52
5
\$\begingroup\$

Recursive calls to loop over the same code again and again are an anti-pattern. If you let the program open for too long, it will die off eventually with a recursion error. Instead, use a while loop:

def main():
    while True:
        tag = input("TAG: ")
        if tag == '/exit':
            answerexit = input("Are you sure? ")
            if  answerexit == 'yes':
                print("")
                print("SHUTTING DOWN..")
                print("")
                time.sleep(2)
                break
            elif answerexit == 'no':
                print("")
                print("SHUTDOWN ABORTED..")
                print("")
        else:
            handle_tag(tag)
            time.sleep(1)


def handle_tag(tag):
    # put all your `if`s in there

A second thing that I don't get is the whole username/password thing. It's not used anywhere... Ho, yes, you greet the user, I see little too no point with that.

Third, the "manual" part, that is printed upfront would be better as a module docstring. Then you could just:

print(__doc__)

Lastly, I would put the tags description into a dictionary, it will let you add stuff painlessly as you wouldn't have to add a new if each time.

TAGS = {
    '<p>': '''
...''',
    '<h>': '''
....''',
...
}


def handle_tag(tag):
    try:
        print(TAGS[tag])
    except KeyError:
        print('unknown tag', tag)
\$\endgroup\$
3
  • \$\begingroup\$ could you edit the entire code to show what you mean? I am lost. \$\endgroup\$
    – EgMusic
    Sep 16, 2017 at 17:24
  • \$\begingroup\$ @ChaseBarnes what part don't you get exactly? 1) remove username/passwords 2) use a dictionary 3) use a while loop (which I provided the code for)? \$\endgroup\$ Sep 16, 2017 at 20:39
  • \$\begingroup\$ I don't get the dictionary. I am not going to remove the Username/Password function because I plan to have a good purpose for that. It's just not finished yet. And I implemented your while True loop! It works like a charm! \$\endgroup\$
    – EgMusic
    Sep 18, 2017 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.