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


Messages In This Thread
return outside function - by seamus - May-15-2019, 07:57 PM
RE: return outside function - by ichabod801 - May-15-2019, 08:14 PM
RE: return outside function - by seamus - May-16-2019, 07:01 PM
RE: return outside function - by ichabod801 - May-16-2019, 08:02 PM
RE: return outside function - by seamus - May-17-2019, 07:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of function/return Paulman 6 2,475 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Multiple return from function Grimmar 7 3,705 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  Lambda function not return value mbilalshafiq 4 3,419 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,303 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,877 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  [split] problem with function return value ops 1 3,443 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 5,094 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,318 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Need of return in function if statement inside the function already returns Athul 5 4,019 Aug-16-2018, 10:19 AM
Last Post: DuaneJack
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,275 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