0

Current output: 'a _ _ l e

Desired output: 'a _ _ l e'

I am unsure how to get the quotation mark at the end of the output in the for loop. This is a function for a Hangman game where provided variables are the secretWord and a list of the lettersGuessed.

def getGuessedWord(secretWord, lettersGuessed):

    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
    what letters in secretWord have been guessed so far.
    '''

    print("'",end=""),

    for letter in secretWord:
        if letter in lettersGuessed:
            print (letter, end=''), 
        else:
            print ("_",end=''),

    return letter

getGuessedWord('apple', ['e', 'l', 'k', 'o', 'a', 's'])
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • try using `end='\''` – Tim Feb 17 '21 at 03:45
  • `print("'",end="") ` , read up what is this, try things out before posting anything – not_python Feb 17 '21 at 03:47
  • 2
    @toRex: Your edit was incorrect. You removed the thing the question was about. – user2357112 Feb 17 '21 at 03:47
  • Does this answer your question? [Using quotation marks inside quotation marks](https://stackoverflow.com/questions/9050355/using-quotation-marks-inside-quotation-marks) – Gino Mempin Feb 17 '21 at 03:51
  • I did use print(" ' ", end=" ") but then the function also returns 'None', which is not desired. – Ryan Bingheim Feb 17 '21 at 03:51
  • You seem to be mixing 2 different problems. In this current post, you are asking about printing a single quote at the end of the string. Then you are now asking about the function returning None. If you already tried something, please post your current attempt to clarify the actual problem. – Gino Mempin Feb 17 '21 at 03:56
  • Gino Mempin - Thank you for your feedback. I only raised the issue with None due to the suggested fix not giving the desired result. I was looking for a way to edit the code to produce my desired output without creating a new issue. I am looking through the link your provided and hope to get my answer there. Thank you – Ryan Bingheim Feb 17 '21 at 04:03

1 Answers1

0

There are multiple ways to fix this; one is to specify the single quote character in double quotes, another is to use escape characters. In essence, and escape character is a normal character such as 'a', 'b', 'c', etc. that is following a backslash ('\'). This means that python will treat the escape character as plain text, allowing you to insert anything such as a single quote, or even another backslash, which would be used for printing a single backslash. Another good use is with '\n', which specifies a new line, and '\t' which specifies a tab. Keep in mind that only a single ascii character can be used as an escape character, after which, everything other than the backslash itself and the single quote or double quote is treated as normal text. Hopefully that made sense, if not, consider reading this: https://www.w3schools.com/python/gloss_python_escape_characters.asp

EDIT:

print("\n\t\tHello, World\"")

would print a new line, then a couple of tabs and "Hello, World!" and a double-quote at the end for the record.

TheDude04
  • 107
  • 9
  • 1
    it would have been awesome if you were to provide sample snippets for your suggestions to convey them as clearly as possible and not leave any room for confusion – Tibebes. M Feb 17 '21 at 03:59