Python Forum
Restart Error when using code from lesson book
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Restart Error when using code from lesson book
#2
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
Reply


Messages In This Thread
RE: Restart Error when using code from lesson book - by deanhystad - Mar-11-2020, 05:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 1,845 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  Using a button to kill and restart a script duckredbeard 3 3,439 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  Error: How to to close and restart your shell after running 'conda init' angelica 3 10,400 May-27-2020, 10:00 AM
Last Post: snippsat
  Hotkey to restart code? DannyB 1 2,844 May-20-2020, 02:52 AM
Last Post: michael1789
  Kernel needs to restart ErnestTBass 0 2,407 May-06-2020, 08:37 PM
Last Post: ErnestTBass
  How to restart Python after input change ozstar 3 2,460 Apr-29-2020, 03:16 AM
Last Post: ozstar
  While loop won't restart program :( TheDovah77 4 4,337 Apr-04-2019, 07:37 PM
Last Post: TheDovah77
  Alien game code from book TheMusicMan 4 3,289 Aug-26-2018, 06:05 PM
Last Post: TheMusicMan
  Jupyter error - 'The kernel appears to have died, it will restart automatically' meganhollie 5 18,145 Jun-12-2018, 10:11 PM
Last Post: Larz60+
  pyython code copied 1to1 from book not working. BlackReptile 2 2,999 Feb-21-2018, 02:46 AM
Last Post: abhin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020