Python Forum

Full Version: Restart Error when using code from lesson book
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I am very new to programming but am trying to learn Python. I am using Coding Projects in Python by DK. It's supposed to be a guessing game. Using IDLE, I type in the code which I copied from the book but get this restart error: >>>
= RESTART: C:\Users\kchristy\AppData\Local\Programs\Python\Python38-32\nine_lives.py
>>>
Here is the code I typed in IDLE:

import random

lives = 9
words = ['pizza', 'dance', 'kitty', 'puppy', 'snowy', 'sleep', 'scale', 'tulip', 'cacti', 'plane', 'otter', 'shirt', 'fairy', 'angel', 'plate', 'email','horse']
secret_word = random.choice(words)
clue = list('?????')
heart_symbol = u'\u2764'
guessed_word_correctly = False

def update_clue(guessed_letter, secret_word, clue):
index = 0
while index < len(secret_word):
if guessed_letter == secret_word[index]:
clue[index] = guessed_letter
index = index + 1

while lives > 0:
print(clue)
print('Lives left: ' + heart_symbol * lives)
guess = input('Guess a letter or the whole word: ')

if guess == secret_word:
guessed_word_correctly = True
break

if guess in secret_word:
update_clue(guess, secret_word, clue)
else:
print('Incorrect. You lose a life')
lives = lives - 1

if guessed_word_correctly:
print('You won! The secret word was ' + secret_word)
else:
print ('You lost! The correct word was ' + secret_word)


I appreciate any help! Thank you.
I think you have a problem with your indents as the code runs fine for me after some formatting:
import random

lives = 9
words = ['pizza', 'dance', 'kitty', 'puppy', 'snowy', 'sleep', 'scale', 'tulip', 'cacti', 'plane', 'otter', 'shirt', 'fairy', 'angel', 'plate', 'email','horse']
secret_word = random.choice(words)
clue = list('?????')
heart_symbol = u'\u2764'
guessed_word_correctly = False

def update_clue(guessed_letter, secret_word, clue):
    index = 0
    while index < len(secret_word):
        if guessed_letter == secret_word[index]:
            clue[index] = guessed_letter
        index = index + 1

while lives > 0:
    print(clue)
    print('Lives left: ' + heart_symbol * lives)
    guess = input('Guess a letter or the whole word: ')

    if guess == secret_word:
        guessed_word_correctly = True
        break

    if guess in secret_word:
        update_clue(guess, secret_word, clue)
    else:
        print('Incorrect. You lose a life')
        lives = lives - 1

if guessed_word_correctly:
    print('You won! The secret word was ' + secret_word)
else:
    print ('You lost! The correct word was ' + secret_word)
I'm would like to see your code with indents. I'm curious about the error you are seeing and what could be causing it. Try posting your code surrounded by
When I copied and pasted it I didn't realize that the indents didn't paste properly. On my original, I did indent. But the fact that it worked for you might mean that I don't actually have all the lines correctly indented. I will check and let you know if that changes anything. Thank you.

Thank you. The indents were not all correct. And I think I know why I had trouble reading them. The code examples in the lesson book are in separate blocks so I didn't pay attention to the indents.
At any rate, it seems to work! Except now I will tinker with it. Again, thank you.