从Python中的函数返回列表

从Python中的函数返回列表,python,Python,我正在为我妹妹创建一个游戏,我想要一个函数返回一个列表变量,这样我就可以将它传递给另一个变量 有关守则如下: def startNewGame(): while 1: #Introduction: print print """Hello, You will now be guided through the setup process. There are 7 steps to this. You can c

我正在为我妹妹创建一个游戏,我想要一个函数返回一个列表变量,这样我就可以将它传递给另一个变量

有关守则如下:

def startNewGame():
    while 1:
    #Introduction:
        print
        print """Hello,
    You will now be guided through the setup process.
There are 7 steps to this.
You can cancel setup at any time by typing 'cancelSetup'
        Thankyou"""
    #Step 1 (Name):
        print
        print """Step 1 of 7:
    Type in a name for your PotatoHead:
    """
        inputPHName = raw_input('|Enter Name:|')
        if inputPHName == 'cancelSetup':
            sys.exit()
    #Step 2 (Gender):
        print
        print """Step 2 of 7:
    Choose the gender of your PotatoHead:
                        input either 'm' or 'f'
    """
        inputPHGender = raw_input('|Enter Gender:|')
        if inputPHGender == 'cancelSetup':
            sys.exit()
    #Step 3 (Colour):
        print
        print """Step 3 of 7:
    Choose the colour your PotatoHead will be:
                        Only Red, Blue, Green and Yellow are currently supported
    """
        inputPHColour = raw_input('|Enter Colour:|')
        if inputPHColour == 'cancelSetup':
            sys.exit()
    #Step 4 (Favourite Thing):
        print
        print """Step 4 of 7:
    Type your PotatoHead's favourite thing:
    """
        inputPHFavThing = raw_input('|Enter Favourite Thing:|')
        if inputPHFavThing == 'cancelSetup':
            sys.exit()
    # Step 5 (First Toy):
        print
        print """Step 5 of 7:
    Choose a first toy for your PotatoHead:
    """
        inputPHFirstToy = raw_input('|Enter First Toy:|')
        if inputPHFirstToy == 'cancelSetup':
            sys.exit()
    #Step 6 (Check stats):
        while 1:
            print
            print """Step 6 of 7:
    Check the following details to make sure that they are correct:
    """
            print
            print """Name:\t\t\t""" + inputPHName + """
Gender:\t\t\t""" + inputPHGender + """
Colour:\t\t\t""" + inputPHColour + """
Favourite Thing:\t""" + inputPHFavThing + """
First Toy:\t\t""" + inputPHFirstToy + """
"""
            print
            print "Enter 'y' or 'n'"
            inputMCheckStats = raw_input('|Is this information correct?|')
            if inputMCheckStats == 'cancelSetup':
                sys.exit()
            elif inputMCheckStats == 'y':
                break
            elif inputMCheckStats == 'n':
                print "Re-enter info: ..."
                print
                break
            else:
                "The value you entered was incorrect, please re-enter your choice"
        if inputMCheckStats == 'y':
            break
    #Step 7 (Define variables for the creation of the PotatoHead):
    MFCreatePH = []
    print
    print """Step 7 of 7:
    Your PotatoHead will now be created...

    Creating variables...
    """
    MFCreatePH = [inputPHName, inputPHGender, inputPHColour, inputPHFavThing, inputPHFirstToy]
    time.sleep(1)
    print "inputPHName"
    print
    time.sleep(1)
    print "inputPHFirstToy"
    print
    return MFCreatePH
    print "Your PotatoHead varibles have been successfully created!"
class PotatoHead:
#Initialise the PotatoHead object:
def __init__(self, data):
    self.data = data    #Takes the data from the start new game function - see main.py
    #Defines the PotatoHead starting attributes:
    self.name = data[0]
    self.gender = data[1]
    self.colour = data[2]
    self.favouriteThing = data[3]
    self.firstToy = data[4]
    self.age = '0.0'
    self.education = [self.eduScience, self.eduEnglish, self.eduMaths] = '0.0', '0.0', '0.0'
    self.fitness = '0.0'
    self.happiness = '10.0'
    self.health = '10.0'
    self.hunger = '0.0'
    self.tiredness = 'Not in this version'
    self.toys = []
    self.toys.append(self.firstToy)
    self.time = '0'
    #Sets data lists for saving, loading and general use:
    self.phData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy)
    self.phAdvData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy,
                      self.age, self.education, self.fitness, self.happiness, self.health, self.hunger,
                      self.tiredness, self.toys)
#+--------------------------------------+#
#| potatohead.py                        |#
#| A module for the Potato Head Game    |#
#| Author:                              |#
#| Date Created / Modified:             |#
#|              24/1/10 | 24/1/10       |#
#+--------------------------------------+#  Tested: Yes (24/1/10)

#Create the PotatoHead class:
class PotatoHead:
    #Initialise the PotatoHead object:
    def __init__(self, data):
        self.data = data    #Takes the data from the start new game function - see main.py
        #Defines the PotatoHead starting attributes:
        self.name = data[0]
        self.gender = data[1]
        self.colour = data[2]
        self.favouriteThing = data[3]
        self.firstToy = data[4]
        self.age = '0.0'
        self.education = [self.eduScience, self.eduEnglish, self.eduMaths] = '0.0', '0.0', '0.0'
        self.fitness = '0.0'
        self.happiness = '10.0'
        self.health = '10.0'
        self.hunger = '0.0'
        self.tiredness = 'Not in this version'
        self.toys = []
        self.toys.append(self.firstToy)
        self.time = '0'
        #Sets data lists for saving, loading and general use:
        self.phData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy)
        self.phAdvData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy,
                          self.age, self.education, self.fitness, self.happiness, self.health, self.hunger,
                          self.tiredness, self.toys)

    #Define the phStats variable, enabling easy display of PotatoHead attributes:
    def phDefStats(self):
        self.phStats = """Your Potato Head's Stats are as follows:
----------------------------------------
Name: \t\t""" + self.name + """
Gender: \t\t""" + self.gender + """
Colour: \t\t""" + self.colour + """
Favourite Thing: \t""" + self.favouriteThing + """
First Toy: \t""" + self.firstToy + """
Age: \t\t""" + self.age + """
Education: \t""" + str(float(self.eduScience) + float(self.eduEnglish) + float(self.eduMaths)) + """
-> Science: \t""" + self.eduScience + """
-> English: \t""" + self.eduEnglish + """
-> Maths:   \t""" + self.eduMaths + """
Fitness: \t""" + self.fitness + """
Happiness: \t""" + self.happiness + """
Health: \t""" + self.health + """
Hunger: \t""" + self.hunger + """
Tiredness: \t""" + self.tiredness + """
Toys: \t\t""" + str(self.toys) + """
Time: \t\t""" + self.time + """
"""

    #Change the PotatoHead's favourite thing:
    def phChangeFavouriteThing(self, newFavouriteThing):
        self.favouriteThing = newFavouriteThing
        phChangeFavouriteThingMsg = "Your Potato Head's favourite thing is " + self.favouriteThing + "."

    #"Feed" the Potato Head i.e. Reduce the 'self.hunger' attribute's value:
    def phFeed(self):
        if float(self.hunger) >=3.0:
            self.hunger = str(float(self.hunger) - 3.0)
        elif float(self.hunger) < 3.0:
            self.hunger = '0.0'
        self.time = str(int(self.time) + 1) #Pass time

    #"Exercise" the Potato Head if between the ages of 5 and 25:
    def phExercise(self):
        if float(self.age) < 5.1 or float(self.age) > 25.1:
            print "This Potato Head is either too young or too old for this activity!"
        else:
            if float(self.fitness) <= 8.0:
                self.fitness = str(float(self.fitness) + 2.0)
            elif float(self.fitness) > 8.0:
                self.fitness = '10.0'
        self.time = str(int(self.time) + 1) #Pass time

    #"Teach" the Potato Head:
    def phTeach(self, subject):
        if subject == 'Science':
            if float(self.eduScience) <= 9.0:
                self.eduScience = str(float(self.eduScience) + 1.0)
            elif float(self.eduScience) > 9.0 and float(self.eduScience) < 10.0:
                self.eduScience = '10.0'
            elif float(self.eduScience) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        elif subject == 'English':
            if float(self.eduEnglish) <= 9.0:
                self.eduEnglish = str(float(self.eduEnglish) + 1.0)
            elif float(self.eduEnglish) > 9.0 and float(self.eduEnglish) < 10.0:
                self.eduEnglish = '10.0'
            elif float(self.eduEnglish) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        elif subject == 'Maths':
            if float(self.eduMaths) <= 9.0:
                self.eduMaths = str(float(self.eduMaths) + 1.0)
            elif float(self.eduMaths) > 9.0 and float(self.eduMaths) < 10.0:
                self.eduMaths = '10.0'
            elif float(self.eduMaths) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        else:
            print "That subject is not an option..."
            print "Please choose either Science, English or Maths"
        self.time = str(int(self.time) + 1) #Pass time

    #Increase Health:
    def phGoToDoctor(self):
        self.health = '10.0'
        self.time = str(int(self.time) + 1) #Pass time

    #Sleep: Age, change stats:              #(Time Passes)
    def phSleep(self):
        self.time = '0'                   #Resets time for next 'day' (can do more things next day)
        #Increase hunger:
        if float(self.hunger) <= 5.0:
            self.hunger = str(float(self.hunger) + 5.0)
        elif float(self.hunger) > 5.0:
            self.hunger = '10.0'
        #Lower Fitness:
        if float(self.fitness) >= 0.5:
            self.fitness = str(float(self.fitness) - 0.5)
        elif float(self.fitness) < 0.5:
            self.fitness = '0.0'
        #Lower Health:
        if float(self.health) >= 0.5:
            self.health = str(float(self.health) - 0.5)
        elif float(self.health) < 0.5:
            self.health = '0.0'
        #Lower Happiness:
        if float(self.happiness) >= 2.0:
            self.happiness = str(float(self.happiness) - 2.0)
        elif float(self.happiness) < 2.0:
            self.happiness = '0.0'
        #Increase the Potato Head's age:
        self.age = str(float(self.age) + 0.1)
然后将其传递给从另一个模块导入的另一个函数

from potatohead import *

PotatoHead对象的代码位于上面导入的PotatoHead.py模块中,如下所示:

def startNewGame():
    while 1:
    #Introduction:
        print
        print """Hello,
    You will now be guided through the setup process.
There are 7 steps to this.
You can cancel setup at any time by typing 'cancelSetup'
        Thankyou"""
    #Step 1 (Name):
        print
        print """Step 1 of 7:
    Type in a name for your PotatoHead:
    """
        inputPHName = raw_input('|Enter Name:|')
        if inputPHName == 'cancelSetup':
            sys.exit()
    #Step 2 (Gender):
        print
        print """Step 2 of 7:
    Choose the gender of your PotatoHead:
                        input either 'm' or 'f'
    """
        inputPHGender = raw_input('|Enter Gender:|')
        if inputPHGender == 'cancelSetup':
            sys.exit()
    #Step 3 (Colour):
        print
        print """Step 3 of 7:
    Choose the colour your PotatoHead will be:
                        Only Red, Blue, Green and Yellow are currently supported
    """
        inputPHColour = raw_input('|Enter Colour:|')
        if inputPHColour == 'cancelSetup':
            sys.exit()
    #Step 4 (Favourite Thing):
        print
        print """Step 4 of 7:
    Type your PotatoHead's favourite thing:
    """
        inputPHFavThing = raw_input('|Enter Favourite Thing:|')
        if inputPHFavThing == 'cancelSetup':
            sys.exit()
    # Step 5 (First Toy):
        print
        print """Step 5 of 7:
    Choose a first toy for your PotatoHead:
    """
        inputPHFirstToy = raw_input('|Enter First Toy:|')
        if inputPHFirstToy == 'cancelSetup':
            sys.exit()
    #Step 6 (Check stats):
        while 1:
            print
            print """Step 6 of 7:
    Check the following details to make sure that they are correct:
    """
            print
            print """Name:\t\t\t""" + inputPHName + """
Gender:\t\t\t""" + inputPHGender + """
Colour:\t\t\t""" + inputPHColour + """
Favourite Thing:\t""" + inputPHFavThing + """
First Toy:\t\t""" + inputPHFirstToy + """
"""
            print
            print "Enter 'y' or 'n'"
            inputMCheckStats = raw_input('|Is this information correct?|')
            if inputMCheckStats == 'cancelSetup':
                sys.exit()
            elif inputMCheckStats == 'y':
                break
            elif inputMCheckStats == 'n':
                print "Re-enter info: ..."
                print
                break
            else:
                "The value you entered was incorrect, please re-enter your choice"
        if inputMCheckStats == 'y':
            break
    #Step 7 (Define variables for the creation of the PotatoHead):
    MFCreatePH = []
    print
    print """Step 7 of 7:
    Your PotatoHead will now be created...

    Creating variables...
    """
    MFCreatePH = [inputPHName, inputPHGender, inputPHColour, inputPHFavThing, inputPHFirstToy]
    time.sleep(1)
    print "inputPHName"
    print
    time.sleep(1)
    print "inputPHFirstToy"
    print
    return MFCreatePH
    print "Your PotatoHead varibles have been successfully created!"
class PotatoHead:
#Initialise the PotatoHead object:
def __init__(self, data):
    self.data = data    #Takes the data from the start new game function - see main.py
    #Defines the PotatoHead starting attributes:
    self.name = data[0]
    self.gender = data[1]
    self.colour = data[2]
    self.favouriteThing = data[3]
    self.firstToy = data[4]
    self.age = '0.0'
    self.education = [self.eduScience, self.eduEnglish, self.eduMaths] = '0.0', '0.0', '0.0'
    self.fitness = '0.0'
    self.happiness = '10.0'
    self.health = '10.0'
    self.hunger = '0.0'
    self.tiredness = 'Not in this version'
    self.toys = []
    self.toys.append(self.firstToy)
    self.time = '0'
    #Sets data lists for saving, loading and general use:
    self.phData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy)
    self.phAdvData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy,
                      self.age, self.education, self.fitness, self.happiness, self.health, self.hunger,
                      self.tiredness, self.toys)
#+--------------------------------------+#
#| potatohead.py                        |#
#| A module for the Potato Head Game    |#
#| Author:                              |#
#| Date Created / Modified:             |#
#|              24/1/10 | 24/1/10       |#
#+--------------------------------------+#  Tested: Yes (24/1/10)

#Create the PotatoHead class:
class PotatoHead:
    #Initialise the PotatoHead object:
    def __init__(self, data):
        self.data = data    #Takes the data from the start new game function - see main.py
        #Defines the PotatoHead starting attributes:
        self.name = data[0]
        self.gender = data[1]
        self.colour = data[2]
        self.favouriteThing = data[3]
        self.firstToy = data[4]
        self.age = '0.0'
        self.education = [self.eduScience, self.eduEnglish, self.eduMaths] = '0.0', '0.0', '0.0'
        self.fitness = '0.0'
        self.happiness = '10.0'
        self.health = '10.0'
        self.hunger = '0.0'
        self.tiredness = 'Not in this version'
        self.toys = []
        self.toys.append(self.firstToy)
        self.time = '0'
        #Sets data lists for saving, loading and general use:
        self.phData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy)
        self.phAdvData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy,
                          self.age, self.education, self.fitness, self.happiness, self.health, self.hunger,
                          self.tiredness, self.toys)

    #Define the phStats variable, enabling easy display of PotatoHead attributes:
    def phDefStats(self):
        self.phStats = """Your Potato Head's Stats are as follows:
----------------------------------------
Name: \t\t""" + self.name + """
Gender: \t\t""" + self.gender + """
Colour: \t\t""" + self.colour + """
Favourite Thing: \t""" + self.favouriteThing + """
First Toy: \t""" + self.firstToy + """
Age: \t\t""" + self.age + """
Education: \t""" + str(float(self.eduScience) + float(self.eduEnglish) + float(self.eduMaths)) + """
-> Science: \t""" + self.eduScience + """
-> English: \t""" + self.eduEnglish + """
-> Maths:   \t""" + self.eduMaths + """
Fitness: \t""" + self.fitness + """
Happiness: \t""" + self.happiness + """
Health: \t""" + self.health + """
Hunger: \t""" + self.hunger + """
Tiredness: \t""" + self.tiredness + """
Toys: \t\t""" + str(self.toys) + """
Time: \t\t""" + self.time + """
"""

    #Change the PotatoHead's favourite thing:
    def phChangeFavouriteThing(self, newFavouriteThing):
        self.favouriteThing = newFavouriteThing
        phChangeFavouriteThingMsg = "Your Potato Head's favourite thing is " + self.favouriteThing + "."

    #"Feed" the Potato Head i.e. Reduce the 'self.hunger' attribute's value:
    def phFeed(self):
        if float(self.hunger) >=3.0:
            self.hunger = str(float(self.hunger) - 3.0)
        elif float(self.hunger) < 3.0:
            self.hunger = '0.0'
        self.time = str(int(self.time) + 1) #Pass time

    #"Exercise" the Potato Head if between the ages of 5 and 25:
    def phExercise(self):
        if float(self.age) < 5.1 or float(self.age) > 25.1:
            print "This Potato Head is either too young or too old for this activity!"
        else:
            if float(self.fitness) <= 8.0:
                self.fitness = str(float(self.fitness) + 2.0)
            elif float(self.fitness) > 8.0:
                self.fitness = '10.0'
        self.time = str(int(self.time) + 1) #Pass time

    #"Teach" the Potato Head:
    def phTeach(self, subject):
        if subject == 'Science':
            if float(self.eduScience) <= 9.0:
                self.eduScience = str(float(self.eduScience) + 1.0)
            elif float(self.eduScience) > 9.0 and float(self.eduScience) < 10.0:
                self.eduScience = '10.0'
            elif float(self.eduScience) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        elif subject == 'English':
            if float(self.eduEnglish) <= 9.0:
                self.eduEnglish = str(float(self.eduEnglish) + 1.0)
            elif float(self.eduEnglish) > 9.0 and float(self.eduEnglish) < 10.0:
                self.eduEnglish = '10.0'
            elif float(self.eduEnglish) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        elif subject == 'Maths':
            if float(self.eduMaths) <= 9.0:
                self.eduMaths = str(float(self.eduMaths) + 1.0)
            elif float(self.eduMaths) > 9.0 and float(self.eduMaths) < 10.0:
                self.eduMaths = '10.0'
            elif float(self.eduMaths) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        else:
            print "That subject is not an option..."
            print "Please choose either Science, English or Maths"
        self.time = str(int(self.time) + 1) #Pass time

    #Increase Health:
    def phGoToDoctor(self):
        self.health = '10.0'
        self.time = str(int(self.time) + 1) #Pass time

    #Sleep: Age, change stats:              #(Time Passes)
    def phSleep(self):
        self.time = '0'                   #Resets time for next 'day' (can do more things next day)
        #Increase hunger:
        if float(self.hunger) <= 5.0:
            self.hunger = str(float(self.hunger) + 5.0)
        elif float(self.hunger) > 5.0:
            self.hunger = '10.0'
        #Lower Fitness:
        if float(self.fitness) >= 0.5:
            self.fitness = str(float(self.fitness) - 0.5)
        elif float(self.fitness) < 0.5:
            self.fitness = '0.0'
        #Lower Health:
        if float(self.health) >= 0.5:
            self.health = str(float(self.health) - 0.5)
        elif float(self.health) < 0.5:
            self.health = '0.0'
        #Lower Happiness:
        if float(self.happiness) >= 2.0:
            self.happiness = str(float(self.happiness) - 2.0)
        elif float(self.happiness) < 2.0:
            self.happiness = '0.0'
        #Increase the Potato Head's age:
        self.age = str(float(self.age) + 0.1)
但是,当我运行程序时,会出现以下错误:

Traceback (most recent call last):
 File "/Users/Jasper/Documents/Programming/Potato Head Game/Current/main.py", line 158, in <module>
myPotatoHead = PotatoHead(MCreatePH)
 File "/Users/Jasper/Documents/Programming/Potato Head Game/Current/potatohead.py", line 15, in __init__
self.name = data[0]
TypeError: 'NoneType' object is unsubscriptable
inputPHName

输入第一玩具

然后它会转到回溯

-----编辑2-----

这就是我正在运行的完整代码:

#+--------------------------------------+#
#| main.py                              |#
#| A main module for the Potato Head    |#
#|  Game to pull the other modules      |#
#|  together and control through user   |#
#|  input                               |#
#| Author:                |#
#| Date Created / Modified:             |#
#|              3/2/10 | 20/2/10         |#
#+--------------------------------------+#  Tested: No

#Import the required modules:
import time
import random
import sys
from potatohead import *
from toy import *

#Start the Game:
def welcomeMessage():
    print "----- START NEW GAME -----------------------"
    print "==Print Welcome Message=="
    print "loading... \t loading... \t loading..."
    time.sleep(1)
    print "loading..."
    time.sleep(1)
    print "LOADED..."
    print; print; print;
    print """Hello,
    Welcome to the Potato Head Game. In this game you can create
a Potato Head, and look after it, like a Virtual Pet.
    This game is constantly being updated and expanded. Please
look out for updates.
"""

#Choose whether to start a new game or load a previously saved game:
def startGame():
    while 1:
        print "--------------------"
        print """ Choose an option:
New_Game
or
Load_Game
"""
        startGameInput = raw_input('>>> >')
        if startGameInput == 'New_Game':
            startNewGame()
            break
        elif startGameInput == 'Load_Game':
            print "This function is not yet supported"
            print "Try Again"
            print
        else:
            print "You must have mistyped the command: Type either 'New_Game' or 'Load_Game'"
            print

#Set the new game up:
def startNewGame():
    while 1:
    #Introduction:
        print
        print """Hello,
    You will now be guided through the setup process.
There are 7 steps to this.
You can cancel setup at any time by typing 'cancelSetup'
        Thankyou"""
    #Step 1 (Name):
        print
        print """Step 1 of 7:
    Type in a name for your PotatoHead:
    """
        inputPHName = raw_input('|Enter Name:|')
        if inputPHName == 'cancelSetup':
            sys.exit()
    #Step 2 (Gender):
        print
        print """Step 2 of 7:
    Choose the gender of your PotatoHead:
                        input either 'm' or 'f'
    """
        inputPHGender = raw_input('|Enter Gender:|')
        if inputPHGender == 'cancelSetup':
            sys.exit()
    #Step 3 (Colour):
        print
        print """Step 3 of 7:
    Choose the colour your PotatoHead will be:
                        Only Red, Blue, Green and Yellow are currently supported
    """
        inputPHColour = raw_input('|Enter Colour:|')
        if inputPHColour == 'cancelSetup':
            sys.exit()
    #Step 4 (Favourite Thing):
        print
        print """Step 4 of 7:
    Type your PotatoHead's favourite thing:
    """
        inputPHFavThing = raw_input('|Enter Favourite Thing:|')
        if inputPHFavThing == 'cancelSetup':
            sys.exit()
    # Step 5 (First Toy):
        print
        print """Step 5 of 7:
    Choose a first toy for your PotatoHead:
    """
        inputPHFirstToy = raw_input('|Enter First Toy:|')
        if inputPHFirstToy == 'cancelSetup':
            sys.exit()
    #Step 6 (Check stats):
        while 1:
            print
            print """Step 6 of 7:
    Check the following details to make sure that they are correct:
    """
            print
            print """Name:\t\t\t""" + inputPHName + """
Gender:\t\t\t""" + inputPHGender + """
Colour:\t\t\t""" + inputPHColour + """
Favourite Thing:\t""" + inputPHFavThing + """
First Toy:\t\t""" + inputPHFirstToy + """
"""
            print
            print "Enter 'y' or 'n'"
            inputMCheckStats = raw_input('|Is this information correct?|')
            if inputMCheckStats == 'cancelSetup':
                sys.exit()
            elif inputMCheckStats == 'y':
                break
            elif inputMCheckStats == 'n':
                print "Re-enter info: ..."
                print
                break
            else:
                "The value you entered was incorrect, please re-enter your choice"
        if inputMCheckStats == 'y':
            break
    #Step 7 (Define variables for the creation of the PotatoHead):
    MFCreatePH = []
    print
    print """Step 7 of 7:
    Your PotatoHead will now be created...

    Creating variables...
    """
    MFCreatePH = [inputPHName, inputPHGender, inputPHColour, inputPHFavThing, inputPHFirstToy]
    time.sleep(1)
    print "inputPHName"
    print
    time.sleep(1)
    print "inputPHFirstToy"
    print
    return MFCreatePH
    print "Your PotatoHead varibles have been successfully created!"

#Run Program:
welcomeMessage()
MCreatePH = startGame()
myPotatoHead = PotatoHead(MCreatePH)
potatohead.py模块如下所示:

def startNewGame():
    while 1:
    #Introduction:
        print
        print """Hello,
    You will now be guided through the setup process.
There are 7 steps to this.
You can cancel setup at any time by typing 'cancelSetup'
        Thankyou"""
    #Step 1 (Name):
        print
        print """Step 1 of 7:
    Type in a name for your PotatoHead:
    """
        inputPHName = raw_input('|Enter Name:|')
        if inputPHName == 'cancelSetup':
            sys.exit()
    #Step 2 (Gender):
        print
        print """Step 2 of 7:
    Choose the gender of your PotatoHead:
                        input either 'm' or 'f'
    """
        inputPHGender = raw_input('|Enter Gender:|')
        if inputPHGender == 'cancelSetup':
            sys.exit()
    #Step 3 (Colour):
        print
        print """Step 3 of 7:
    Choose the colour your PotatoHead will be:
                        Only Red, Blue, Green and Yellow are currently supported
    """
        inputPHColour = raw_input('|Enter Colour:|')
        if inputPHColour == 'cancelSetup':
            sys.exit()
    #Step 4 (Favourite Thing):
        print
        print """Step 4 of 7:
    Type your PotatoHead's favourite thing:
    """
        inputPHFavThing = raw_input('|Enter Favourite Thing:|')
        if inputPHFavThing == 'cancelSetup':
            sys.exit()
    # Step 5 (First Toy):
        print
        print """Step 5 of 7:
    Choose a first toy for your PotatoHead:
    """
        inputPHFirstToy = raw_input('|Enter First Toy:|')
        if inputPHFirstToy == 'cancelSetup':
            sys.exit()
    #Step 6 (Check stats):
        while 1:
            print
            print """Step 6 of 7:
    Check the following details to make sure that they are correct:
    """
            print
            print """Name:\t\t\t""" + inputPHName + """
Gender:\t\t\t""" + inputPHGender + """
Colour:\t\t\t""" + inputPHColour + """
Favourite Thing:\t""" + inputPHFavThing + """
First Toy:\t\t""" + inputPHFirstToy + """
"""
            print
            print "Enter 'y' or 'n'"
            inputMCheckStats = raw_input('|Is this information correct?|')
            if inputMCheckStats == 'cancelSetup':
                sys.exit()
            elif inputMCheckStats == 'y':
                break
            elif inputMCheckStats == 'n':
                print "Re-enter info: ..."
                print
                break
            else:
                "The value you entered was incorrect, please re-enter your choice"
        if inputMCheckStats == 'y':
            break
    #Step 7 (Define variables for the creation of the PotatoHead):
    MFCreatePH = []
    print
    print """Step 7 of 7:
    Your PotatoHead will now be created...

    Creating variables...
    """
    MFCreatePH = [inputPHName, inputPHGender, inputPHColour, inputPHFavThing, inputPHFirstToy]
    time.sleep(1)
    print "inputPHName"
    print
    time.sleep(1)
    print "inputPHFirstToy"
    print
    return MFCreatePH
    print "Your PotatoHead varibles have been successfully created!"
class PotatoHead:
#Initialise the PotatoHead object:
def __init__(self, data):
    self.data = data    #Takes the data from the start new game function - see main.py
    #Defines the PotatoHead starting attributes:
    self.name = data[0]
    self.gender = data[1]
    self.colour = data[2]
    self.favouriteThing = data[3]
    self.firstToy = data[4]
    self.age = '0.0'
    self.education = [self.eduScience, self.eduEnglish, self.eduMaths] = '0.0', '0.0', '0.0'
    self.fitness = '0.0'
    self.happiness = '10.0'
    self.health = '10.0'
    self.hunger = '0.0'
    self.tiredness = 'Not in this version'
    self.toys = []
    self.toys.append(self.firstToy)
    self.time = '0'
    #Sets data lists for saving, loading and general use:
    self.phData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy)
    self.phAdvData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy,
                      self.age, self.education, self.fitness, self.happiness, self.health, self.hunger,
                      self.tiredness, self.toys)
#+--------------------------------------+#
#| potatohead.py                        |#
#| A module for the Potato Head Game    |#
#| Author:                              |#
#| Date Created / Modified:             |#
#|              24/1/10 | 24/1/10       |#
#+--------------------------------------+#  Tested: Yes (24/1/10)

#Create the PotatoHead class:
class PotatoHead:
    #Initialise the PotatoHead object:
    def __init__(self, data):
        self.data = data    #Takes the data from the start new game function - see main.py
        #Defines the PotatoHead starting attributes:
        self.name = data[0]
        self.gender = data[1]
        self.colour = data[2]
        self.favouriteThing = data[3]
        self.firstToy = data[4]
        self.age = '0.0'
        self.education = [self.eduScience, self.eduEnglish, self.eduMaths] = '0.0', '0.0', '0.0'
        self.fitness = '0.0'
        self.happiness = '10.0'
        self.health = '10.0'
        self.hunger = '0.0'
        self.tiredness = 'Not in this version'
        self.toys = []
        self.toys.append(self.firstToy)
        self.time = '0'
        #Sets data lists for saving, loading and general use:
        self.phData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy)
        self.phAdvData = (self.name, self.gender, self.colour, self.favouriteThing, self.firstToy,
                          self.age, self.education, self.fitness, self.happiness, self.health, self.hunger,
                          self.tiredness, self.toys)

    #Define the phStats variable, enabling easy display of PotatoHead attributes:
    def phDefStats(self):
        self.phStats = """Your Potato Head's Stats are as follows:
----------------------------------------
Name: \t\t""" + self.name + """
Gender: \t\t""" + self.gender + """
Colour: \t\t""" + self.colour + """
Favourite Thing: \t""" + self.favouriteThing + """
First Toy: \t""" + self.firstToy + """
Age: \t\t""" + self.age + """
Education: \t""" + str(float(self.eduScience) + float(self.eduEnglish) + float(self.eduMaths)) + """
-> Science: \t""" + self.eduScience + """
-> English: \t""" + self.eduEnglish + """
-> Maths:   \t""" + self.eduMaths + """
Fitness: \t""" + self.fitness + """
Happiness: \t""" + self.happiness + """
Health: \t""" + self.health + """
Hunger: \t""" + self.hunger + """
Tiredness: \t""" + self.tiredness + """
Toys: \t\t""" + str(self.toys) + """
Time: \t\t""" + self.time + """
"""

    #Change the PotatoHead's favourite thing:
    def phChangeFavouriteThing(self, newFavouriteThing):
        self.favouriteThing = newFavouriteThing
        phChangeFavouriteThingMsg = "Your Potato Head's favourite thing is " + self.favouriteThing + "."

    #"Feed" the Potato Head i.e. Reduce the 'self.hunger' attribute's value:
    def phFeed(self):
        if float(self.hunger) >=3.0:
            self.hunger = str(float(self.hunger) - 3.0)
        elif float(self.hunger) < 3.0:
            self.hunger = '0.0'
        self.time = str(int(self.time) + 1) #Pass time

    #"Exercise" the Potato Head if between the ages of 5 and 25:
    def phExercise(self):
        if float(self.age) < 5.1 or float(self.age) > 25.1:
            print "This Potato Head is either too young or too old for this activity!"
        else:
            if float(self.fitness) <= 8.0:
                self.fitness = str(float(self.fitness) + 2.0)
            elif float(self.fitness) > 8.0:
                self.fitness = '10.0'
        self.time = str(int(self.time) + 1) #Pass time

    #"Teach" the Potato Head:
    def phTeach(self, subject):
        if subject == 'Science':
            if float(self.eduScience) <= 9.0:
                self.eduScience = str(float(self.eduScience) + 1.0)
            elif float(self.eduScience) > 9.0 and float(self.eduScience) < 10.0:
                self.eduScience = '10.0'
            elif float(self.eduScience) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        elif subject == 'English':
            if float(self.eduEnglish) <= 9.0:
                self.eduEnglish = str(float(self.eduEnglish) + 1.0)
            elif float(self.eduEnglish) > 9.0 and float(self.eduEnglish) < 10.0:
                self.eduEnglish = '10.0'
            elif float(self.eduEnglish) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        elif subject == 'Maths':
            if float(self.eduMaths) <= 9.0:
                self.eduMaths = str(float(self.eduMaths) + 1.0)
            elif float(self.eduMaths) > 9.0 and float(self.eduMaths) < 10.0:
                self.eduMaths = '10.0'
            elif float(self.eduMaths) == 10.0:
                print "Your Potato Head has gained the highest level of qualifications in this subject! It cannot learn any more!"
        else:
            print "That subject is not an option..."
            print "Please choose either Science, English or Maths"
        self.time = str(int(self.time) + 1) #Pass time

    #Increase Health:
    def phGoToDoctor(self):
        self.health = '10.0'
        self.time = str(int(self.time) + 1) #Pass time

    #Sleep: Age, change stats:              #(Time Passes)
    def phSleep(self):
        self.time = '0'                   #Resets time for next 'day' (can do more things next day)
        #Increase hunger:
        if float(self.hunger) <= 5.0:
            self.hunger = str(float(self.hunger) + 5.0)
        elif float(self.hunger) > 5.0:
            self.hunger = '10.0'
        #Lower Fitness:
        if float(self.fitness) >= 0.5:
            self.fitness = str(float(self.fitness) - 0.5)
        elif float(self.fitness) < 0.5:
            self.fitness = '0.0'
        #Lower Health:
        if float(self.health) >= 0.5:
            self.health = str(float(self.health) - 0.5)
        elif float(self.health) < 0.5:
            self.health = '0.0'
        #Lower Happiness:
        if float(self.happiness) >= 2.0:
            self.happiness = str(float(self.happiness) - 2.0)
        elif float(self.happiness) < 2.0:
            self.happiness = '0.0'
        #Increase the Potato Head's age:
        self.age = str(float(self.age) + 0.1)
#+--------------------------------------+#
#|马铃薯过敏|#
#|土豆头游戏的模块|#
#|作者:|#
#|创建/修改日期:|#
#|              24/1/10 | 24/1/10       |#
#+--------------------------------------+#测试:是(2010年1月24日)
#创建PotatoHead类:
PotatoHead类:
#初始化PotatoHead对象:
定义初始化(自身,数据):
self.data=data#从开始新游戏功能获取数据-请参阅main.py
#定义PotatoHead起始属性:
self.name=数据[0]
self.gender=数据[1]
self.color=数据[2]
self.favoriteThing=数据[3]
self.firstToy=数据[4]
self.age='0.0'
self.education=[self.educhscience,self.educenglish,self.educhmath]='0.0','0.0','0.0'
self.fitness='0.0'
自我幸福='10.0'
self.health='10.0'
self.hunger='0.0'
self.tiredness='不在此版本中'
self.toys=[]
self.toys.append(self.firstToy)
self.time='0'
#设置用于保存、加载和一般用途的数据列表:
self.phData=(self.name、self.gender、self.color、self.favoriteThing、self.firstToy)
self.phAdvData=(self.name、self.gender、self.color、self.favoriteThing、self.firstToy、,
自我年龄、自我教育、自我健身、自我幸福、自我健康、自我饥饿、,
自我疲劳,自我玩具)
#定义phStats变量,以便轻松显示PotatoHead属性:
def phDefStats(自身):
self.phStats=“”您的土豆头的统计信息如下:
----------------------------------------
名称:\t\t“”+self.Name+“”
性别:\t\t”“+self.Gender+“”
颜色:\t\t”“+self.color+“”
最喜欢的东西:\t“”+self.favoriteThing+“”
第一个玩具:\t”“+self.firstToy+“”
年龄:\t\t”“+self.Age+“”
教育:\t”“+str(float(self.eduScience)+float(self.edusenglish)+float(self.edusmath))+“”
->科学:\t”“+self.eduScience+“”
->英语:\t”“+self.eduEnglish+“”
->数学:\t”“+self.edumath+“”
适合度:\t“”+self.Fitness+“”
幸福:\t“”+自我幸福+“”
运行状况:\t“”+self.Health+“”
饥饿:\t“”+自我。饥饿+“”
疲劳:\t“”+自我。疲劳+“”
玩具:\t\t”“+str(self.Toys)+”
时间:\t\t”“+self.Time+“”
"""
#改变土豆头最喜欢的东西:
def PHChangeFavoriteThing(自我、新收藏夹):
self.favoriteThing=newfavoriteThing
phChangeFavoriteThingMsg=“你的土豆头最喜欢的东西是“+self.favoriteThing+”
#“喂”土豆头,即降低“自我饥饿”属性的值:
def phFeed(自):
如果浮动(自我饥饿)>=3.0:
self.hunger=str(浮动(self.hunger)-3.0)
elif浮动(自我饥饿)<3.0:
self.hunger='0.0'
self.time=str(int(self.time)+1)#通过时间
#如果年龄在5岁至25岁之间,则“锻炼”土豆头:
def phExercise(自我):
如果浮动(自身年龄)<5.1或浮动(自身年龄)>25.1:
打印“此土豆头对于此活动来说太年轻或太老!”
其他:
如果浮动(自适配)8.0:
self.fitness='10.0'
self.time=str(int(self.time)+1)#通过时间
#“教”土豆头:
def phTeach(自我、科目):
如果主题=‘科学’:
如果float(self.eduScience)9.0和float(self.eduScience)<10.0:
self.eduScience='10.0'
elif浮动(self.eduScience)==10.0:
打印“你的土豆头在这门学科中获得了最高水平的资格!它不能再学习了!”
elif subject=='English':
如果float(self.eduEnglish)9.0和float(self.eduEnglish)<10.0:
self.eduEnglish='10.0'
elif浮动(self.eduEnglish)==10.0:
打印“你的土豆头在这门学科中获得了最高水平的资格!它不能再学习了!”
elif科目==“数学”:
如果float(self.eduMaths)9.0和float(self.eduMaths)<10.0:
self.edumathm='10.0'
elif浮点(self.edumath)==10.0:
打印“你的土豆头在这门学科中获得了最高水平的资格!它不能再学习了!”
其他:
打印“该主题不是选项…”
打印“请选择科学、英语或数学”
self.time=str(int(self.time)+1)#通过时间
#增进健康:
def phGoToDoctor(自身):
self.health='10.0'
self.time=str(int(self.time)+1)#通过时间
#睡眠:年龄,更改统计信息:#(时间流逝)
def phSleep(自我):
self.time='0'#重置下一天的时间(第二天可以做更多事情)
#增加饥饿:
如果浮动(自我饥饿)5.0:
self.hunger='10.0'
#低适应度:
如果浮动(自适配度)>=0.5:
self.fitness=str(浮动(self.fitness)-0.5)
elif浮动(自适配度)<0.5:
self.fitness='0.0'
#低健康:
如果浮动