Miles Procise
Miles Procise

Reputation: 9

Eliminating last element in array

So I am working on a small hangman text based game.

The problem I am currently dealing with is calling random words from my text file. Each word has one additional character for a new line (\n).

For instance, running through my function that separates a string's letters into individual elements I get something to the effect of:

from text file: guess
answer = arrange_word(guess)
>>>>> ['g', 'u', 'e', 's', 's', '\n']

however, when joining the array back together the following is shown:

print ''.join(arrange_word)
>>>>> guess

as you can see, it is a bit difficult to guess an element that does not show up.

For clarity here is my function for arrange_word:

def arrange_word(word):
##########
# This collects the mystery word and breaks it into an array of
# individual letters.
##########
     word_length = len(word)
     break_up = ["" for x in range(word_length)]
     for i in range(0, word_length):
          break_up[i] = word[i]
     return break_up

What I am stuck on is that when trying to guess letters, the \n is impossible to guess. The win condition of my game is based on the guess being identical to the answer word. However the \n keeps that from working because they are of different length.

These answer arrays are of different length as well, since I am just pulling random lines from a text file of ~1000 words. After hours of searching I cannot seem to find out how to drop the last element of an array.

Upvotes: 0

Views: 93

Answers (6)

idjaw
idjaw

Reputation: 26570

For this line here:

word_length = len(word)

Before you take the length, what you can do is this first:

word = word.strip()

Explanation:

strip removes leading and trailing whitespace.

>>> s = "bob\n"
>>> s
'bob\n'
>>> s.strip()
'bob'

With all this in mind, you don't need the rest of this code anymore:

 word_length = len(word)
 break_up = ["" for x in range(word_length)]
 for i in range(0, word_length):
      break_up[i] = word[i]

Applying the strip will give you your word without the whitespace character, then all you want to do after this to have a list of characters, is simply:

>>> s = "bob"
>>> list(s)
['b', 'o', 'b']

So your method can now simply be:

def arrange_word(word):
    return list(word.strip())

Demo:

arrange_word("guess")

Output:

['g', 'u', 'e', 's', 's']

Upvotes: 3

Adam Acosta
Adam Acosta

Reputation: 603

All these answers are fine for specifically stripping whitespace characters from a string, but more generally, Python lists implement standard stack/queue operations, and you can make your word into a list just by calling the list() constructor without needing to write your own function:

In [38]: letters = list('guess\n')
         letters.pop()
         letters
Out[38]: ['g', 'u', 'e', 's', 's']

Upvotes: 1

poko
poko

Reputation: 575

Maybe first line of your arrange_word function should be

word = word.strip()

to remove all leading/trailing whitespace characters.

Upvotes: 0

Julien Spronck
Julien Spronck

Reputation: 15423

Just strip the word:

word = 'guess\n'
word = word.strip()  ## 'guess' without new line character, or spaces

Upvotes: 0

R. Farahani
R. Farahani

Reputation: 13

you can access last element by -1 index like: guess[-1] and you can delte it by:

del guess[-1] 

Upvotes: 0

mrhn
mrhn

Reputation: 18916

Use List slicing

arr = [1,2,3,4]

print(arr[:-1:])

Array slicing syntax is [startindex:endindex:offset(2, means each 2 element)] So in your case you could. Which mean start at the begging of the list, to the last element -1 for every 1 element in the list.

return break_up[:-1:]

Upvotes: 0

Related Questions