Python Forum

Full Version: return outside function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anyone please help i am a newbi although an OAP.please see program below copied from "the python book"I have tried indenting and unindenting
Using Python 2

from random import *
player_score = 0
computer_score = 0
def hangedman(hangman):
 graphic = [   
        """    

            +-------+
            |
            |
            |
            |
        ==================
        """
            ,
        """



           +--------+
           |        |
           |        0
           |
           |
           |
        ====================
        """
           ,
        """  
          

           +--------+
           |        |
           |        0
           |        |
           |
           |
        ==================
        """
            ,
        """

           

           +--------+
           |        |
           |        0
           |       -|
           |
           |
        ==================
        """
            ,
        """

           

           +--------+
           |        |
           |        0
           |       -|-

           |
           |
        ==================
        """
            ,
        """

           

           +--------+
           |        |
           |        0
           |       -|-
           |       /
           |
        ==================
        """
            ,
        """

           

           +---------+
           |         |
           |         0
           |        -|-
           |        / \
           |
        ===================
        """]

print graphic[hangman]  
return      


def start():
    print "Let's play a game of hangman"
    while game():
        pass
    scores()

def game():
    dictionary = ["gnu","kernel","linux", "mageia","penguin","ubuntu"]
    word = choice(dictionary)
    word_length = len(word)
    clue = word_length *["_"]    
    tries = 6
    letters_tried = ""
    guesses = 0
    letters_right = 0
    letters_wrong = 0
    global computer_score, player_score

    while (letters_wrong != tries) and ("".join(clue) != word):
        letter=guess_letter()
        if len(letter)==1 and letter.isalpha():
            if letters_tried.find(letter) != -1:
                print "You've already picked", letter
            else:
                letters_tried = letters_tried + letter
                first_index = word.find(letter)
                if first_index == -1:
                    letters_wrong +=1
                    print "Sorry,",letter,"isn't what were looking for"
                else:
                    print "Congratulations.",letter, "is correct"
                    for i in range(word_length):
                        if letter == word(i):
                            clue[i] = letter
        else:
                 print "Choose another"

        hangedman(letters_wrong)
        print"".join(clue)
        print "Guesses: ",letters_tried

        if letters_wrong == tries:
                print "Game over"
                print "The word was". word
                player_score += 1
                break
            
        if "".join(clue) == word:
                print "You win"
                print "The word was",word
                player_score += 1
                break               
            
            
        def guess_letter():
            print
            letter = raw_input("Take a guess at the mystery word.")
            letter_strip()
            letter_lower()
            print
            return letter

        def play_again():
            answer = raw_input("Would you like to play again y/n:")
            if answer in ("y","Y","yes","Yes", "of course!"):
                return answer
            else:
                print "Thank you very much for playing our game. see you next time!"

        def scores():
            global player_score, computer_score
            print "HIGH SCORES"
            print "Player",player_score
            print "Compter", computer_score

            if __name__ == "__main__":
                start()
You need to indent both lines 94 and 95, and it needs to match the indent on line 5 (which does not match your other indents, so maybe you should change that first).

In the future, please give the full text of any error you are getting.
Thanks for your advice it did indeed solve the problem. Now as you can see when i try to run the program i get a prompt to enter a letter but when i try i get the following, i get the feeling i am being stupid but if you can advise i would be grateful. Oh and i will definitely look at the sites you suggested. Thanks again for your advice it is appreciated.

Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "copyright", "credits" or "license()" for more information.
>>>
==================== RESTART: /home/jjb/python/hangman.py ====================
>>> o

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
o
NameError: name 'o' is not defined
>>>
==================== RESTART: /home/jjb/python/hangman.py ====================
>>>
>>>
You are not entering the letter into a prompt, you are entering it in to the interactive Python shell. I don't think your program is running at all. I think you need to unindent the last two lines of your program so that the if __name__ is flush right and the next line is indented once.

Indentation is incredibly important in Python. It determines when each line of code is run, if at all. You need to pay more attention to it.
Thanks again for your advice