5
\$\begingroup\$

I need someone to review and improve this code.

import time
import os, signal

User_Name = (input("What is your name?: ")) # If blank twice, sets to John
if User_Name == '':
    print("Please enter a name")
    time.sleep(1)
    User_Name = (input("What is your name?: "))
    if User_Name == '':
        User_Name = "John"


Computer_Name = (input("What will my name be?: ")) # If blank twice, sets to HELPER
if Computer_Name == '':
    print("Please enter a name")
    time.sleep(1)
    Computer_Name = (input("What will my name be?: "))
    if Computer_Name == '':
        Computer_Name = "HELPER"

def GREET():
    print('Hey, ' + User_Name + '!' + ' I am ' + Computer_Name + '! What can I help you today?')
    print("(Please use HTML tags WITH < and > when using this help tool.)")

def HELPTOOL():
    User_Input = input(User_Name + ": ")

    if User_Input == 'help':
        print("")
        print(User_Name + ", please enter a tag, such as <p>, ")
        time.sleep(0.1)
        print("to display what that tag is.")
        time.sleep(0.1)
        print("")
        print("Type 'about' or 'contact' for more information ")
        time.sleep(0.1)
        print("")
        HELPTOOL()
    elif User_Input == 'restart':
        time.sleep(0.1)
        print("")
        print("RESTARTING..")
        print("")
        time.sleep(0.1)
        time.sleep(0.1)
        time.sleep(0.1)
        time.sleep(0.1)
        time.sleep(0.1)
        for i in range(1):
            GREET()
    elif User_Input == 'about':
        print("")
        print("This tool was orginally made to help HTML progr")
        time.sleep(0.1)
        print("ammers understand the different tags in the gre")
        time.sleep(0.1)
        print("at language of HTML")
        time.sleep(0.1)
        print("")
        HELPTOOL()
    elif User_Input == 'contact':
        print('')
        time.sleep(0.1)
        print("Email: ****@gmail.com")
        time.sleep(0.1)
        print("")
        HELPTOOL()
    elif User_Input == '<p>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("The <p> tag defines a PARAGRAPH. Browsers autom")
        time.sleep(0.1) 
        print("aticlly add some space (margin) before and afte")
        time.sleep(0.1)
        print("r each <p> element. The margins can be modified")
        time.sleep(0.1)
        print("with CSS (with the margin properties)")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<div>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("The <div> tag is used to create a 'box' that co")
        time.sleep(0.1)
        print("ntains everything that you put between these ta")
        time.sleep(0.1)
        print("gs: <div> </div>. This can be used for creating")
        time.sleep(0.1)
        print("interactive menus, navigations bars, and more!")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == 'tags':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("Tags. They are the very foundation of this lang")
        time.sleep(0.1)
        print("uage itself! These tags are the hidden elements")
        time.sleep(0.1)
        print("that define how your web browser must format an")
        time.sleep(0.1)
        print("d display the content. Nearly all these 'tags' ")
        time.sleep(0.1)
        print("have two parts: The opening tag EX. <p>, and Th")
        time.sleep(0.1)
        print("e closing tag EX. </p>. These create all of the")
        time.sleep(0.1)
        print("content you see on sites such as Google.")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<!--...-->':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("The <!--...--> tag (or comment tag) creates a c")
        time.sleep(0.1)
        print("omment, and therefore hiding that text or code ")
        time.sleep(0.1)
        print("from the browser. Comments can be used for a va")
        time.sleep(0.1)
        print("riety of things, from saving certain bits of co")
        time.sleep(0.1)
        print("de for later, to explaining what something does")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<!DOCTYPE html>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("This tag is the declaration of an HTML document")
        time.sleep(0.1)
        print(". It tells the browser 'Hey, im an HTML Docume")
        time.sleep(0.1)
        print("nt!' (Not literally)")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<a>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("This tag defines a hyperlink. This is used when")
        time.sleep(0.1)
        print("you want to create text that, when clicked, wil")
        time.sleep(0.1)
        print("l redirect you to a differect page, or download")
        time.sleep(0.1)
        print("a file.")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<abbr>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("<abbr> is used to abbreveate things, such as CD")
        time.sleep(0.1)
        print("C or FBI.")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == 'exit':
        print("Shutting Down...")
        time.sleep(1.5)
        os.kill(os.getpid(), signal.SIGTERM)
    elif User_Input == '<h>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("The <h> tag is used to create a header, which i")
        time.sleep(0.1)
        print("s text that is seperated from the rest, and app")
        time.sleep(0.1)
        print("ears at the top of the webpage or document.")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<address>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("This tag defines the contact information, or AD")
        time.sleep(0.1)
        print("DRESS of the owner of the document. ")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<b>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("This defines BOLD text. This has a specific use")
        time.sleep(0.1)
        print(", such as (<p>Hello <b>World</b>!</p>)")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<body>':
        print("-----------------------------------------------")
        time.sleep(0.1)
        print("Defines the BODY of an HTML document. ")
        time.sleep(0.1)
        print("-----------------------------------------------")
        HELPTOOL()
    elif User_Input == '<br>':
        print("-----------------------------------------------")
        time.sleep(0.1)
    else:
        time.sleep(0.1)
        print("Sorry, I didn't catch that..")
        time.sleep(0.1)
        HELPTOOL()

GREET()
HELPTOOL()

This code's purpose is to help HTML programmers grasp the knowledge of the HTML tags, and how to use them.

\$\endgroup\$
2
  • \$\begingroup\$ when the function prints the text into the console, it prints it one line at a time instead of all of it instantly. \$\endgroup\$
    – EgMusic
    Commented Sep 13, 2017 at 19:50
  • 3
    \$\begingroup\$ I'm begging of you please follow PEP8 guidelines. (especially) In Python readability counts! \$\endgroup\$
    – bergerg
    Commented Sep 13, 2017 at 22:21

3 Answers 3

8
\$\begingroup\$

Let's look first at the inputs that you have.

User_Name = (input("What is your name?: ")) # If blank twice, sets to John
if User_Name == '':
    print("Please enter a name")
    time.sleep(1)
    User_Name = (input("What is your name?: "))
    if User_Name == '':
        User_Name = "John"


Computer_Name = (input("What will my name be?: ")) # If blank twice, sets to HELPER
if Computer_Name == '':
    print("Please enter a name")
    time.sleep(1)
    Computer_Name = (input("What will my name be?: "))
    if Computer_Name == '':
        Computer_Name = "HELPER"

my first question, why do you need the time.sleep(1)? This will force your code to wait and does not seem to serve a purpose here. You have parenthesis around the input statements that are not needed.

Second, it's important to recognize that this is a repeated block of code, if this was a function it would work a lot better:

def getDefault(question, default):
    User_Name = input(question) # removed extra parenthesis, question is now a variable
    if User_Name == '':
        print("Not recognized!")#changed to be more ambiguous
        time.sleep(1)
        User_Name = input(question)#removed parens
        if User_Name == '':
            User_Name = "John"

    return User_Name

But what if we want to force the user to give us an input we can use as a name? We can keep asking indefinite number of times until we get an answer?

Here is a straightforward function that will keep querying a user until a valid response is recieved. it will allow special characters/numbers in input.

def askUser(question):
    response = ''    #start with an empty string
    while response == '':   #until we get a non empty response
        response = input(question) #keep asking the user
    return response #when we get an answer return it

called as:

userName = askUser("What is your name?")

computerName = askUser("What is my name?")

You can add print statements and error messages and some more if/else flow control inside the while loop to make the interface with the user prettier but the code above will solve the problem of a user not giving a response.

In your HELPTOOL function you have a giant if/elif/elif/else block. I think that this can be made much simpler with a dictionary. It gets rid of a lot of the extraneous if lines. Again I would recommend removing the time.sleep commands, as they don't serve a purpose in this use case.

here is an example of how I would use a dictionary for a large if block.

Highly recommended reading: https://docs.python.org/3/tutorial/datastructures.html#dictionaries

my_dict = { #note the curly braces used for a dictionary


    'this_is_a_key': "this is the value which is found using the key",
    'restart' : "Restarting text goes here",  #must put comma after each entry
    '<p>': "My paragraph entry text",
    'tags': "my tags text",
    '<div>': """If I want to have text and I want it to have
                Special block formatting I can use triple quotes
                This way python will care about how I have indented
                and written my string out"""
    '<a>': "If I want to have something on a new line I can use \n, the special character for printing on the next line. Other cool ones: \t, \r"

   #and so on, filling all of your text entries

}

this way, when the user tells me to explain something like '< p>', instead of having to write dozens of if statements I can just do:

my_dict['< p>'] and it will give me back the entry I put for '< p>'

I can also be very safe and say my_dict.get('< p>', None), this way if I try to find something that isn't there, (or if the user asks for something I don't know about) it will just get me nothing and I can do checks to make sure that I tell the user I don't know it.

For example:

text = my_dict.get('< p>', None)
if text is None:
    print("OH no! Not recognized!")
else:    #implies that the text is NOT None, and so we can print it
    print(text)

Also worth looking at is a cool string feature.

in python:

>>a = "-"
>>b = a*10
>>print(b)
"----------"

This saves you the tedium of writing 30 characters that are the same. Instead you can multiply it and you are given back that string repeated that number of times. See if you can find a use case in your code!

\$\endgroup\$
2
  • \$\begingroup\$ return User_Name gives a SyntaxError \$\endgroup\$
    – EgMusic
    Commented Sep 14, 2017 at 15:04
  • 1
    \$\begingroup\$ That was an indenting issue in my code, you can fix it by moving the return line over 4 spaces. Note, sometimes code uses tabs or 4 spaces. It is better to use 4 spaces. Here is the python style guide which argues the case for spaces: python.org/dev/peps/pep-0008/#tabs-or-spaces \$\endgroup\$
    – Erich
    Commented Sep 14, 2017 at 17:04
3
\$\begingroup\$

You might be able to refactor a little, by making those sleeps a function that you call, but I don't know if that's worthwhile. Why do you have time.sleep(0.1) several times in a row, rather than time.sleep(0.5)?

Also, you have:

print("This tool was orginally made to help HTML progr")
        time.sleep(0.1)
        print("ammers understand the different tags in the gre")
        time.sleep(0.1)

Which breaks the line in a strange place. Irregardless of your choice of line break placement, you might think about adding `, end="") to your print statements, which avoids the automatic newline after the print statement. eg

print("Dead", end="")
print(" parrot!", end="")

will output:

Dead Parrot!

to the console all on one line.

\$\endgroup\$
1
  • \$\begingroup\$ "Why do you have time.sleep(0.1) several times in a row, rather than time.sleep(0.5)?" I am lazy. I have time.sleep in my clipboard. And look at the code where it displays the text. I was trying to not go past that certain limit, because I always have IDLE as a half window, no full-screen. \$\endgroup\$
    – EgMusic
    Commented Sep 13, 2017 at 17:55
1
\$\begingroup\$

I don't understand the usefulness of this program. In my opinion you should extract all the text from the program, put it into a text document (MarkDown is a popular and simple format) and then delete the program. This has several benefits:

  • It allows full-text search.
  • I don't have to enter personal information to access public data.
  • It's easily extendable and modifiable.
  • It's faster because text documents don't sleep.
  • There are programs that generate a table of contents from a text document, for a quick overview. Your program doesn't have that currently.
\$\endgroup\$
0

Your Answer

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

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