Python Forum
return outside function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return outside function
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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 ====================
>>>
>>>
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks again for your advice
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of function/return Paulman 6 2,307 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Multiple return from function Grimmar 7 3,490 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  Lambda function not return value mbilalshafiq 4 3,252 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,182 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,598 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  [split] problem with function return value ops 1 3,279 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 4,828 Jan-28-2020, 06:20 PM
Last Post: snippsat
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 5,181 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Need of return in function if statement inside the function already returns Athul 5 3,868 Aug-16-2018, 10:19 AM
Last Post: DuaneJack
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,168 Jul-01-2018, 01:17 AM
Last Post: juliabrushett

Forum Jump:

User Panel Messages

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