-1

I am writing this code while learning from online videos. The issue as after running the code I am getting errors with the last else indentation and the print("string",end = ""). I just can't figure out the end error that keeps popping.

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 Number
    secret_word = random.choice(words)
    bad_guesses = []
    good_guesses = []
    while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_words)):
    # Draw guesses 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))

This is the error I am getting:

line 17 print(letter, end = "") ^ SyntaxError: invalid syntax Process finished with exit code 1

And regarding the Python version I am trying on both 2.7 and 3.0.

When I removed the end = "" the program ran , but broke on return.

Jason
  • 2,278
  • 2
  • 17
  • 25
Ahmad Hussien
  • 29
  • 2
  • 7
  • The `print(string, [end])` syntax only works in Python 3, as that's the only version where `print` is a function. What are you using? – Akshat Mahajan Apr 16 '16 at 23:07
  • The code has a plain `input` that is allowed to return `'q'` so it must be Python 3. – Alex Hall Apr 16 '16 at 23:08
  • Also, it is a good idea to always include a copy of the error traceback rather than expect us to run your code and reproduce the same error. – Akshat Mahajan Apr 16 '16 at 23:08
  • 1
    check your indentation - specifically your while and for statements at lines 14-16. – Stidgeon Apr 16 '16 at 23:09
  • 1
    @AlexHall OP says he's following instructions off a video tutorial. If it's copied wholesale, the presence of `input` doesn't rule out the possibility of an incorrect version. :) – Akshat Mahajan Apr 16 '16 at 23:10
  • 3
    *“I am getting errors”* – Please include the actual error messages and stack traces. – poke Apr 16 '16 at 23:12
  • When I ran this program I found that it told me when I guessed word correctly it also told me I didn't get it and sometimes it allows for more than 7 guesses. – ruthless Apr 16 '16 at 23:35
  • This is the error message, out of the PyCharm IDE: line 17 print(letter, end = "") ^ SyntaxError: invalid syntax Process finished with exit code 1. and I tried it on both python 2.7 and python 3.0 – Ahmad Hussien Apr 17 '16 at 06:58
  • Your program is broken. 1. indentation is wrong everywhere. 2. should be `len(list(secret_word))` without `s`. 3. the final `else` doesn't match anywhere (where's `if` clause??). Make it correct then discuss about this. ps. The syntax must be Python 3.x. – knh170 Apr 17 '16 at 07:38
  • @knh170 the `else` clause is part of the `while` loop: http://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement – tynn Apr 17 '16 at 07:51
  • 1
    @AhmadHussien please **edit** the question to add the stack trace. Now you've actually ruined the stack trace and we cannot see where the ^ is pointing at! – Antti Haapala -- Слава Україні Apr 17 '16 at 07:55
  • However, this error is consistent with what I get when running Python 2. Install **Python 3** (3.5, not 3.0) and use it to run the program. – Antti Haapala -- Слава Україні Apr 17 '16 at 07:59

1 Answers1

0

You are using python 3 print syntax in python 2.7 . You could do that if you add from __future__ import print_function. So your script would start like this

from __future__ import print_function 
import random
# Make a list of words
words = ['apple','banana','orange','coconut','strawberry','lime','grapefruit','lemon','kumquat', 'blueberry','melon']

Other than that, you need to fix a typo in line 13 (secret_words => secret_word)

while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):

Then your program runs fine in python 3, here is a working example: https://repl.it/CG46

Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
  • Thank you very much for your help and support (for everyone as well). So it was mainly a future import issue, thanks a bunch. – Ahmad Hussien Apr 18 '16 at 06:45