RhysBuddy
RhysBuddy

Reputation: 145

extra apostrophes string python

Whenever I replace one of the values in my array/list, the string that I try to replace it with gets extra apostrophes and or commas that I don't want. For example, I'm trying to assign index 2 of

guessHistory = ['','','','','','','','']

to the return value of this function:

def compareWords(word1, word2):
    result = 0
    if word1[0] == word2[0]:
        result +=1
    if word1[1] == word2[1]:
        result += 1
    if word1[2] == word2[2]:
        result += 1
    if word1[3] == word2[3]:
        result += 1
    if word1[4] == word2[4]:
        result += 1
    if word1[5] == word2[5]:
        result += 1
    print result, '/ 6 correct. \n'
    return result

I do this by

guessHistory[guessNum] = '[', compareWords(wordList[guessNum], password) , '/6 correct]'

and so i get this as a result
example of text

but id like it to be [3/6 correct] instead of ('[', 3, '/6 correct]')

guessHistoryIndex = 0
for index, item in enumerate(wordList, 1):
    print index, ' )', item, '   ' , guessHistory[guessHistoryIndex]
    guessHistoryIndex += 1

this is the loop that the index changes within

Upvotes: 0

Views: 393

Answers (1)

John Kugelman
John Kugelman

Reputation: 361849

guessHistory[guessNum] = '[', compareWords(wordList[guessNum], password) , '/6 correct]'

By using commas you're creating a tuple with several elements, and tuples look ugly when printed. You can combine the pieces together into a single string using str.format:

guessHistory[guessNum] = '[{0}/6 correct]'.format(compareWords(wordList[guessNum], password))

This will look much better.

Upvotes: 2

Related Questions