Python: Index Error: sequence subscript is out of range - python

I am trying to implement a word Chain game; it is a word game where players take turns saying words that start with the last letter of the previous word.
This is my code, and I keep getting an index error on line 9. Can anyone please help. Thanks
This is for Python 3:
x = input('Word: ')
xx = 'sample'
while x and xx:
if x == '':
break
xx = input('Word: ')
if xx == '':
break
while xx[0] != x[-1]:
print('Invalid word')
xx = input('Word: ')
x = input('Word: ')
if x == '':
break
while x[0] != xx[-1]:
print('Invalid word')
x = input('Word: ')
Here is the criteria
Word Chain is word game where players take turns saying words that start with the last letter of the previous word. You might have played this game on long car trips.
Write a program to help you play word chain. Your program should read in words until a blank line is entered. It should print out Invalid word if a word is not a valid play. Your program should work for upper case and lower case words.
​
Notice that the word mandarin is rejected because it doesn't start with the letter e from the previous word: orange. The next word still needs to start with the letter e (from orange), rather than n (from the end of the invalid word, mandarin).
Here is another example:
Word: tomato
Word: okra
Word: asparagus
Word: seaweed
Word: cake
Invalid word
Word: dried apricots
Word: cake
Invalid word
Word:
​
Here's one last example. Don't forget it should work regardless of case!
Word: Australia
Word: Antartic
Word: Canada
Word: England
Invalid word
Word: Denmark
Invalid word
Word:
​
You will always read in at least two words.
Please help if you can thank you

You can simply include your condition in the while loop and simply your whole code like the following:
word = "elaborate"
old_word = 'sample'
while word != '' and old_word != '':
old_word = word
word = input('Word: ')
if word[0].upper() != old_word[-1].upper():
print('Invalid word')
word = input('Word: ')

This should work, check the comments in the code! You would need to check if the word you have entered is of length > 1, and also keep track of the last good word!
old_word = 'sample'
word = input('Start the chain game, the first word is sample: ')
last_good_word = word
invalid_flag = False
#While the word is not empty
while word != '':
#If the last word was invalid, use the last good word, else get the latest word
if invalid_flag:
old_word = last_good_word
else:
old_word = word
#We get the new word
word = input('Enter the next word: ')
#Check if the new word is not a blank string
if len(word) > 1:
#If word chain is not met, tell that it is invalid, and set the invalid flag to True
if word[0].upper() != old_word[-1].upper():
print('Invalid word')
invalid_flag = True
last_good_word = old_word
#Else set the invalid flag to False
else:
invalid_flag = False
Outputs will look like
Start the chain game, the first word is sample: echo
Enter the next word: open
Enter the next word: note
Enter the next word: even
Enter the next word:
Start the chain game, the first word is sample: echo
Enter the next word: open
Enter the next word: yellow
Invalid word
Enter the next word: mellow
Invalid word
Enter the next word: note
Enter the next word: even
Enter the next word: yellow
Invalid word
Enter the next word: mellow
Invalid word
Enter the next word: never
Enter the next word: river
Enter the next word:

Related

How do I parse a string without running into infinite loops?

Hello everyone I have a question regarding a parsing of string and this is the code I have so far:
sentence =' '
while sentence != 'q':
sentence = input('Please enter your input: ').split(',')
if len(sentence) > 1:
print('First word: {sentence[0]}')
print('Second word: {sentence[1]}')
continue
elif len(sentence) == 1:
print('Error no comma.')
continue
elif sentence == 'q':
break
And the output will be if there is no comma inputted will give me the following:
Enter input string:
Jill Allen
Error: No comma in string.
and it will keep on asking me for a string until I entered q for quit and the program exits as follows:
Enter input string:
Jill, Allen
First word: Jill
Second word: Allen
Enter input string:
Golden , Monkey
First word: Golden
Second word: Monkey
Enter input string:
Washington,DC
First word: Washington
Second word: DC
Enter input string:
q
My problem is I am unable to quit the infinite loop. Can anyone help me on this? I think the program is unable to distinguish 'Jill Allen' from 'q' since both of them have len(sentence) == 1 and that is the problem as it gives me Error no comma and asked for an input over and over.
I think the key to understanding your problem may be here:
sentence = input('Please enter your input: ').split(',')
You are storing the user input in a string, but then you are calling the string method str.split(). This method basically returns a list of sub-strings, based on the separator you pass as an argument. If there is no separator, the method will instead create a list whose only element is the original input string.
You can find more information about this method here: https://docs.python.org/3/library/stdtypes.html#str.split.
So, if your input is "q", separator will be storing the array ["q"], as there is no comma. And this array's length is 1, so it will enter the first "elif", and execute the "continue", therefore ending current iteration.
In absence of further information about your project, if you need to do it this way, you can change the order of the last two conditionals and the break conditional itself in order for it to work:
sentence =' '
while True:
sentence = input('Please enter your input: ').split(',')
if len(sentence) > 1:
print(f'First word: {sentence[0]}')
print(f'Second word: {sentence[1]}')
continue
elif sentence[0] == 'q':
break
elif len(sentence) == 1:
print('Error no comma.')
continue
I also changed the while condition, because it was redundant with the "break" statement.
You have to change the places of the last two if statements. Because the last if statement never gets executed as if you split sentence which is just q, its length is 1.
sentence =' '
while sentence != 'q':
sentence = input('Please enter your input: ').split(',')
if len(sentence) > 1:
print('First word: {sentence[0]}')
print('Second word: {sentence[1]}')
continue
elif sentence == 'q':
break
elif len(sentence) == 1:
print('Error no comma.')
continue
Check this code. It's working fine for me.
def wordPrinter(text):
if len(sentence) > 1:
print(f'First word: {sentence[0]}')
print(f'Second word: {sentence[1]}')
elif len(sentence) == 1:
print('Error no comma.')
while True:
sentence = input('Please enter your input: ').split(',')
if sentence == ["q"]:
break
else:
wordPrinter(sentence)
As you splitting the input from very beginning you have to compare string by list element.
Code:
sentence =' '
while True:
sentence = input('Please enter your input: ').split(',') #['q']
if len(sentence) > 1:
print(f'First word: {sentence[0]}')
print(f'Second word: {sentence[1]}')
continue
elif sentence == ['q']:
break
else:
print('Error no comma.')
continue
while (sentence := input('Please enter your input: ')) != 'q':
first, *rest = sentence.split(',')
if rest:
print(f'First word: {first}')
second, *rest = rest
print(f'Second word: {second}')
if rest:
print(f'Rest: {",".join(rest)}')
else:
print('Error no comma.')
You can use Python 3.8's walrus operator to assign the input to a variable and also check it's value. I then used iterable unpacking to get the first element of the str.split and the rest as a list. Checking a list itself in a condition tells you if it has any elements in it. I did the same for the second word. Additionally I'm also printing the rest of the words if there are any.
when you use split() you will take list, so sentence cant be str('q')
try like this:
sentence=' '
while True:
sentence = input().split(',')
if (len(sentence) == 1)&(sentence[0] == 'q'):
break
elif len(sentence)<=1:
print('error')
else:
print(f'First word: {sentence[0]}')
print(f'Second word: {sentence[1]}')

How do you insert repeated values with distinct indices into an empty list in python?

I'm trying to build a basic program where the computer selects a word out of a pre-existing list (called "words") and the user must guess the appropriate letters to guess the word. This is what the main function looks like so far:
def game():
word = random.choice(words)
while ' ' or '-' in word:
word = random.choice(words)
if ' ' or '-' not in word:
break
print(f'Hint: The chosen word is {len(word)} letters long')
letters = list(word)
progress = []
while True:
guess = str(input('Guess a letter: '))
if len(guess) > 1:
print('Sorry, guess a single letter: ')
if guess in word:
print(f'The letter {guess} is in the word')
for i, j in enumerate(letters):
if progress.count(guess) >= letters.count(guess):
break
elif j == guess:
progress.insert(i, j)
print('Current progress: ' + '-'.join(progress))
if len(progress) == len(word):
if letters[:] == progress[:]:
print('Congrats! You found the word: ' + str(word))
break
elif guess not in word:
print(f'The letter {guess} is not in the word: Try Again')
continue
My issue is with the for loop where I use enumerate(y) and the respective "elif j == guess" condition. I noticed that when running the function, the code works if the letters that are repeated are successive (ex: in the word "chilly", if I type in "l", the function correctly displays the two l's and the game works as intended). However, if the letters are repeated separately (ex: the word "cologne"), the function doesn't insert the "l" between the two o's, and keeps the two o's together regardless, thus preventing the proper word from ever being guessed. Is there a different method I could use to fix this problem?
You should remember the letters already guessed and simplyfiy the printing to any letter that you remember and use - for any other letter in the word.
Your errror stems from your list and counting method to remember which letters to print or not.
I fixed your incorrect if-condition (see How to test multiple variables against a value? for more on that).
import random
# supply list of words to function to avoid globals
def game(words):
word = random.choice(words)
# fixed your check ... not sure why you do that
while ' ' in word or '-' in word:
word = random.choice(words)
# no break needed, will break if no space/- in
print(f'Hint: The chosen word is {len(word)} letters long')
# remember which letters where guessed already
guesses = set()
while True:
guess = input('Guess a letter: ') # no str needed it is already a str
if len(guess) > 1:
print('Sorry, guess a single letter: ')
continue # back to while loop
# add to guessed letters
guesses.add(guess)
# print message
if guess in word:
print(f'The letter {guess} is in the word')
else:
print(f'The letter {guess} is not in the word: Try Again')
continue # back to while loop
print('Current progress: ', end="")
# assume we have all letters guessed
done = True
for idx, letter in enumerate(word):
if letter in guesses:
# print letter if already guessed
print(letter, end="")
else:
# invalidate assumption and print -
done = False
print("-",end="")
print()
# if assumption not invalidated: done
if done:
print('Congrats! You found the word: ' + str(word))
break
game(["eye", "ear", "egg", "anvil"])
Output:
Hint: The chosen word is 3 letters long
Guess a letter: The letter e is in the word
Current progress: e-e
Guess a letter: The letter r is not in the word: Try Again
Current progress: e-e
Guess a letter: The letter y is in the word
Current progress: eye
Congrats! You found the word: eye

Word list storage returning "string index out of range". Why?

I am working on an assignment where we have to ask the user for a word, and then if the word has a letter that repeats the initial letter in the word, such as: ApplesAuce (the A repeats), the program will store the word in the list and then print out the list when the user is done entering words.
I am getting this error
if word[0].lower() in word[1:].lower(): IndexError: string index out
of range
Here is my code:
wordlist = []
word = input("Please enter a hilariously long word: ")
# I said "hilariously long" to increase the likelihood of a repeat letter
while wordlist != '':
word = input("Please enter another hilariously long word: ")
if word[0].lower() in word[1:].lower():
wordlist.append(word)
word = input("Please enter another hilariously long word: ")
print("All of the words that had repeated first letters are: ")
print(wordlist)
Test for the presence of a word and break out of the while loop if it isn't there.
wordlist = []
msg = "Please enter a hilariously long word: "
# I said "hilariously long" to increase the likelihood of a repeat letter
while True:
word = input(msg)
if word:
if word[0].lower() in word[1:].lower():
wordlist.append(word)
else:
break
print("All of the words that had repeated first letters are: ")
print(wordlist)
Note also that wordlist is a list so the test while wordlist != "" will always be the case because a list is not a string
This should work.I have introduced the breaker that is quit or done will break the loop. I have also moved your first input inside which will add another if wordlist is populated.
wordlist = []
# I said "hilariously long" to increase the likelihood of a repeat letter
while 1:
word = input("Please enter {}hilariously long word: ".format('another ' if wordlist else ''))
# get out if done or quit is typed
if word in ('done','quit'):
break
if word[0].lower() in word[1:].lower():
wordlist.append(word)
print("All of the words that had repeated first letters are: ")
print(wordlist)

using a while True loop

Problem 1.
This problem provides practice using a while True loop. Write a function named
twoWords that gets and returns two words from a user. The first word is of a
specified length, and the second word begins with a specified letter.
The function twoWords takes two parameters:
an integer, length, that is the length of the first word and
a character, firstLetter, that is the first letter of the second word.
The second word may begin with either an upper or lower case instance of
firstLetter. The function twoWords should return the two words in a list. Use
a while True loop and a break statement in the implementation of twoWords. The
following is an example of the execution of twoWords:
print(twoWords(4, 'B')):
A 4-letter word please two
A 4-letter word please one
A 4-letter word please four
A word beginning with B please apple
A word beginning with B please pear
A word beginning with B please banana
['four', 'banana']
This is what I have so far, but it keeps asking me the first question over again
even if I have the right word length. What am I doing wrong?
def twoWords(length, firstLetter):
wordLen = input('A 4-letter word please ')
while len(wordLen) != int(length):
wordlen = input('A 4-letter word please ')
word = input('A word beginning with ' + firstLetter + ' please ')
while word[0] != firstLetter:
word = input('A word beginning with ' + firstLetter + ' please ')
return[wordLen, word]
def twoWords(length,firstLetter):
firstWord = ""
secondWord= ""
while True:
firstWord = input('A ' + str(length) + '-letter word please.')
if length == len(firstWord):
break
while True:
secondWord = input('A word beginning with ' + firstLetter+ ' please')
if secondWord[0] == firstLetter.upper() or secondWord[0] == firstLetter.lower():
break
return [firstWord,secondWord]
print(twoWords(4,'B'))
def twoWord(length, firstLetter):
while True:
word1=(input("Enter a "+ str(length)+"-letter word please:"))
if len(word1)==int(length):
break
while True:
word2=input("Please enter the word beginning with "+firstLetter+" please:")
if word2[0].upper()==firstLetter or word2[0].lower()==firstLetter:
break
return word1, word2
print(twoWord(5, 'b'))

How do I repeat the program so its asks for a another word and let it only print my sentences once?

How do I repeat the program so its asks for a another word and let it only print my sentences once?
usersentence= input("Please type in a sentence without punctuation: ")
word= input("Please enter a word you want to search for in the sentence: ")
words = usersentence.split(' ')
for (i, words) in enumerate(words):
if (words == word):
print("your word appears in position(s): ")
print(i+1)
else:
print("Sorry yout word does not seem to be here")
To repeat the code, we use loops. You can use for or while loop depending on your problem. If you wont to repeat part of your code a specific number of times, for loop is your answer. For example:
usersentence= input("Please type in a sentence without punctuation: ")
for i in range(3): # number of words you wont to search
word= input("Please enter a word you want to search for in the sentence: ")
words = usersentence.split(' ')
passed = False # Something to check if the word is found
for (i, words) in enumerate(words):
if (words == word):
print("your word appears in position(s): ")
print(i+1)
passed = True # The word has been found
if not passed:
print("Sorry yout word does not seem to be here")

Categories