Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSean Flanagan
33,235 PointsWhy does my program generate an infinite loop?
Hi.
Every time I run my program, for some reason it keeps running and the only way to terminate it is to close Workspaces.
import random
# make a word list
words = [
'apple',
'banana',
'orange',
'coconut',
'strawberry',
'lime',
'grapefruit',
'lemon',
'kumquat',
'blueberry',
'tangarine',
'peach',
'pear',
'blackberry',
'raspberry',
'cranberry',
'melon',
'satsuma',
'raisin',
'hazelnut',
'sultana',
'pineapple'
]
while True:
start = input("Press Enter/Return to start or enter Q to quit")
if start.lower() == 'q':
break
# pick a random word
secret_word = random.choice(words)
bad_guesses = []
good_guesses = []
while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
# draw guessed letters, spaces and strikes
for letter in secret_word:
if letter in good_guesses:
print(letter, end='')
else:
print('_', end='')
print('')
print('Strikes: {}/7'.format(len(bad_guesses)))
print('')
# hazard a guess
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("You can only guess a single letter!")
continue
elif guess in bad_guesses or guess in good_guesses:
print("You've already guessed that letter!")
continue
elif not guess.isalpha():
print("You can only guess letters!")
continue
if guess in secret_word:
good.guesses.append(guess)
if len(good_guesses) == len(list(secret_word)):
print("You win! The word was {}".format(secret_word))
break
else:
bad_guesses.append(guess)
else:
print("You didn't guess it! My secret word was {}".format(secret_word))
# print out win/lose
Does anyone know why this happens please?
Thanks
Sean
2 Answers
Kenneth Love
Treehouse Guest TeacherSo, first thing, near the bottom you have good.guesses.append
. That should be good_guesses.append
.
Oops, hit enter too soon. I'll update this comment when I find what's causing the loop.
The reason for the infinite loop is that you need to indent everything from #hazard a guess
on down by one. That should all be inside of the while
loop.
Also, just so you aren't surprised by it, there's a bug in this code in the course. It gets addressed later on in the course, just don't want you to get hung up on it.
Sean Flanagan
33,235 PointsOk. Thanks Kenneth.
Sean :-)
Sean Flanagan
33,235 PointsSean Flanagan
33,235 PointsHi Kenneth. Thanks for getting back to me.
This is my code now:
Is that right so far?
Cheers
Sean
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherLooks right to me!