-6

My Hangman code has errors on line 40 , line 40

print ('_'+ end=' ')

My code:

import time
import random
print('Lets play hangman')
print("""
       _______
       |    |
       |    O
       |    |
       |  \- -/
       |    |
       |   | |
       |  |   |
   ____|____
   """)



random_words = ['string', 'loop', 'python', 'print', 'run' , 'graphics window', 'variable', 'iteration', 'modules', 'module', 'input', 'logic', 'output    ']
time.sleep(2)
print ("What level would you like to play at? Too bad, you get random! Better type it in tho...")
level_of_difficulty = "Random"
time.sleep(2)
print ("The program is now generating your word...")
if level_of_difficulty == 'Random':
    generated_word = random.choice(random_words)

guessed = ''

lives = 7


while lives > 0:

    missed = 0
    print()
    for letter in generated_word:
        if letter in guessed:
            print("Congrats ya dumb animal, you got one")
        else:
            print ('_'+ end=' ')
            missed = missed + 1
    if missed == 0:
        print ('\n\nYou win!')  
        quit()
        break
    guess=input("Well what are you waiting for? guess a letter!:")

    guessed = guessed + guess
    if guess not in generated_word:
        print ('\n People like you are the reason why the human race will fail')
        print ('Fool')
    lives = lives - 1 
    missed = missed + 1

    print('your new lives value is ' + str(lives))

    if lives < 7:
        print('''   _______
   |    |   ''')
        if lives < 6:
            print('   |    O    ') 
            if lives < 5:            
                print('   |    |    ')
            if lives < 4:
                print('   |  \- -/  ')
            if lives < 3: 
                print('   |    |    ')
            if lives < 2:
                print('   |   | |  ')
            if lives < 1:
                print('   |  |   | ')
            if lives == 0:
             print('___|___      ')

             print('Game Over Boi')
             print('The secret word was ' + str(generated_word) + "fool")
             quit()
Prune
  • 76,765
  • 14
  • 60
  • 81
  • what version of python are you using, and what is the exact error message? – wpercy Nov 09 '18 at 19:51
  • lemme check on that – Timothy Johnson Nov 09 '18 at 19:51
  • 3
    Well, you haven't actually asked a question. You've basically just said, "here's my code, figure out what's wrong." What you should do instead is say, here's my code; i'm getting an error on line XXX, and here's the error message, and here's what I've tried to do to fix it. In other words: https://stackoverflow.com/help/how-to-ask – Kenneth K. Nov 09 '18 at 19:51
  • i am using python in the Enthought Canopy 1.6.4(64 bit) – Timothy Johnson Nov 09 '18 at 19:52
  • `print ('_'+ end=' ')` is a syntax error. You probably just forgot a comma or accidentally typed it as `+` instead, you will want to do: `print ('_', end=' ')`. – Spencer Wieczorek Nov 09 '18 at 19:52
  • 3
    @TimothyJohnson hate is the wrong word, but there are a few things wrong with your post. firstly: please include a Minimal Complete Verifiable Example (the only thing missing is the `minimal` part). secondly, don't call for sympathy with phrases like "I'm new to $Language or $Concept". thirdly, include the errors you're getting, don't just say that you're getting errors. and, lastly: don't ever write things like `please help me, um... yeah`. – Azrael Nov 09 '18 at 19:52
  • %run "C:/Users/mynamehere/hangboi.py" File "C:\Users\mynamehere\hangboi.py", line 40 print ('_'+ end=' ') ^ SyntaxError: invalid syntax – Timothy Johnson Nov 09 '18 at 19:53
  • Spencer Wieczorek,i changed it, but it still says something is wrong, a little arrow is pointing at the equals symbol but i need that – Timothy Johnson Nov 09 '18 at 19:56
  • 1
    @TimothyJohnson if you're using python 2.x you can't pass `end=` as an argument to the print function. – wpercy Nov 09 '18 at 19:57
  • Well, i got a buncha downvotes and still cant figure out how to fix it, thank you for trying to help me @wpercy and Spencer Wieczorek and roganjosh – Timothy Johnson Nov 09 '18 at 20:00
  • @TimothyJohnson As @wpercy mentioned this means you using python 2. Which doesn't support the `end` keyword on the `print` function. In that case just remove it and do `print '_',`. [See this question](https://stackoverflow.com/questions/11266068/python-avoid-new-line-with-print-command) on how to not have `print` give a new line for python 2 (which is why the commas is at the end). – Spencer Wieczorek Nov 09 '18 at 20:06

1 Answers1

0

so here is some code that partially belongs to me

This appears to be the root of the problem. The main issue aside from a type is the use of Python 3 functionality when you are using Python 2.

print ('_'+ end=' ')

Aside from the typo that + needs to be a ,. In Python 2 the end keyword is not used for print, which is only used in python 3. Instead you can do the same thing in Python 2 by doing:

print '_',

The next issue will be with line:

guess=input("Well what are you waiting for? guess a letter!:")

This will give you a error because in Python 2 it tries to evaluate what you entered as an expression instead of a string. Instead you will need to do:

guess=raw_input("Well what are you waiting for? guess a letter!:")

With both these fixes you will have a working game. Although you may want to add in a way to see what letters the user got right.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54