Search code examples
pythonpycharm

Can some explain string literals to me with the provided code?


What do I need to change to make this program work? I am getting:

line 10
SyntaxError: Non-UTF-8 code starting with '\x92' in file C:/Users/RobotAdmin/PycharmProjects/untitled3/dialouge on line 10, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

I have no idea what this means.

def main ():
    phrases = "Thank You Please Hello Hi How Old Are You What Is Your Address"**       # String literal

    print(phrases[17:22])   # This statement prints a specific segment of characters from the string literal
    name = input('What is your name? :')     # This statement prompts the user to enter their name and accepts it
    print(phrases[23:25] + " " + name)     # Retrieves from the string literal,and combines it with the name
    print(phrases[25:41])   # This Statement asks for the user’s age
    age = input('Age in Years? :')  # This statement prompts the user to enter their age and accepts the age the entered
    print(phrases[42:64])   # This segment asks What the user’s address is
    address = input("Street Address, City, State, Country") #This statement asks user to enter their address
    print (""
           "______________________________"
           "|                            |"
           "|",name,"                    |"
           "|                            |"
           "|",age,"                     |"
           "|                            |"
           "|",address,"                 |"
           "|                            |"
           "|                            |"
           "|____________________________|")

Solution

  • The reason for getting your error is because of the character used in your user’s comment which is subtlety different to user's which is standard ASCII. So the easiest solution is to just change those two characters.

    If you'd prefer to keep them, you just need to add a suitable coding to the top of your script:

    # -*- coding: utf-8 -*-
    

    To fix the script output, one solution is to use a dictionary to store your variables. This has two advantages, firstly you can easily determine the length of all of the text that was entered. Secondly it can be used with a format statement to substitute the placeholders in your output.

    With some extra (rather weird) notation, we can get Python to automatically add enough space or underscore padding to all line up.

    def main ():
        phrases = "Thank You Please Hello Hi How Old Are You What Is Your Address"        #**       # String literal
    
        details = dict()
        print(phrases[17:22])   # This statement prints a specific segment of characters from the string literal
        details['name'] = input('What is your name? :')     # This statement prompts the user to enter their name and accepts it
        print(phrases[23:25] + " " + details['name'])     # Retrieves from the string literal,and combines it with the name
        print(phrases[25:41])   # This Statement asks for the user's age
        details['age']  = input('Age in Years? :')  # This statement prompts the user to enter their age and accepts the age the entered
        print(phrases[42:64])   # This segment asks What the user's address is
        details['address'] = input("Street Address, City, State, Country") #This statement asks user to enter their address
    
        # Determine the length of the longest entry
        max_width = max([len(v) for v in details.values()])
    
        print ("""
    __{0:_<{width}}__
    | {0: <{width}} |
    | {name:{width}} |
    | {0: <{width}} |
    | {age:{width}} |
    | {0: <{width}} |
    | {address:{width}} |
    | {0: <{width}} |
    | {0: <{width}} |
    |_{0:_<{width}}_|""".format('', width=max_width, **details))
    
    main()
    

    This would display something like the following output:

    _____________________________
    |                           |
    | Fred Flintstone           |
    |                           |
    | 35                        |
    |                           |
    | Cobblestone Lane, Bedrock |
    |                           |
    |                           |
    |___________________________|