Python

Letterman game dosent work

I thought i followed every step the instructor did but i must have missed something. When i run this in python it says it excpected an indent on import random? ''' import random random_words = ["surface", "awesome", "keyboard", "mouse", "castle"] while True: start = input("Press enter to start, or Q to quit") if start.lower() == "Q": break secret_word = random.choice(random_word) wrong_guesses = [] right_guesses = [] while len(wrong_guesses) < 10 and len(right_guesses) != len(list(secret_word)): for letter in secret_word: if letter in right_guesses: print(letter, end="") else: print("", end="") print("") print("Strikes: {}/7".format(len(wrong_guesses))) print("") guess = input("Guess a letter: ").lower() if len(guess) != 1: print("Your can only guess a single letter!") continue elif guess in wrong_guesses or guess in right_guesses: print("You have already guessed that") elif not guess_isalpha(): print("You can only guess letter!") continue if guess in secretword: right_guesses.append(guess) if len(right_guesses) == len(list(secret_word)): print("You win! The word was {}".format(secret_word)) break else: wrong_guesses.append(guess) else: print("You didnt guess it! My secret word was {}".format(secret_word)) '''

3 Answers

Lets retry. ive tried to follow every instruction during the course and i cant spot any mistakes with this code. However when i run it in python i get an error message saying: IndentationError: expected an indented block(on line 2). What im i missing?

import random
random_words = ["surface", "awesome", "keyboard", "mouse", "castle"]
while True:
    start = input("Press enter to start, or Q to quit")
    if start.lower() == "Q":
        break
    secret_word = random.choice(random_word)
    wrong_guesses = []
    right_guesses = []
    while len(wrong_guesses) < 10 and len(right_guesses) != len(list(secret_word)):
        for letter in secret_word:
            if letter in right_guesses:
                print(letter, end="")
            else:
                print("_", end="")
        print("")
        print("Strikes: {}/7".format(len(wrong_guesses)))
        print("")
        guess = input("Guess a letter: ").lower()
        if len(guess) != 1:
            print("Your can only guess a single letter!")
            continue
        elif guess in wrong_guesses or guess in right_guesses:
            print("You have already guessed that")
        elif not guess_isalpha():
            print("You can only guess letter!")
            continue
        if guess in secret_word:
            right_guesses.append(guess)
            if len(right_guesses) == len(list(secret_word)):
                print("You win! The word was {}".format(secret_word))
                break
        else:
            wrong_guesses.append(guess)
else:
    print("You didnt guess it! My secret word was {}".format(secret_word))
Juan Hurtado
Juan Hurtado
1,764 Points

Your last else statement is the culprit. I just wrote an answer to this question and idk what happened to it.

I just wrote a new version of this that actually runs but it has this annyoing bugs, and i simply dont understand why:

import random

# make a list of words
words = [
        "apple",
        "banana",
        "orange",
        "coconut",
        "strawberry",
        "lime",
        "grapefruit",
        "lemon",
        "kumquat",
        "blueberry",
        "melon",
]

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("")

            # take 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))

I dont guess this, this is EXACLY the same code as provided in the python basics course and yet still it dosent work