我的Python代码没有出现的任何原因?

时间:2013-12-06 23:51:51

标签: python for-loop while-loop

我正在学习如何用Python编写代码并使用IDLE,我已经输入了这段代码,但是当我点击 F5 时,没有任何反应......没有输出。

这可能是因为我输入的代码不需要输出吗?或许我错误地保存了它。很想知道原因,因为它有点令人不安。

X = "X"   #This is to indicate one piece of the game 
O = "O"   #this is to indicate another piece of the game
EMPTY = "" # an empty square on the board.
TIE = "TIE"  #represents a tie game 
NUM_SQUARES = "9" #number of squares on the board

def display_instruct(): #this is a function with the name display_instruct.
    """display game instructions."""
    print \
          """ Welcome to the greatest challenge of all time: Tic-tac toe. This would be a showdown betweene your human brain
and my silcon processor You will mkae your move known by entering a number 

                     0 | 1 | 2
                     ---------
                     3 | 4 | 5
                     ---------
                     6 | 7 |8

        Prepare yourself, human. The ultimate battle is about to begin. \n """

def ask_yes__no(question): 
    """Ask a yes or no question"""
    response = None
    while response not in ("y", "n"):
        response = raw_input(question).lower()
    return response

#this produces a function. It receives a question and thenn responds with an answer which is either yes or not

def ask_number(question, low, high):
    """Ask for a number within the range"""
    response = None
    while response not in range(low, high):
        response - int(raw_input(question))
    return response
    #remember that when defining the functions, you have to put in colons. The user recieves a question and then has to give an answer.

def pieces():
     """Determine if player or computer goes first""" #docstrings are used to name the functions.
    go_first = ask_yes_no("Do you requre the first move?y/n: ")
    if go_first == "y":                        #important to have two equal signs because you are giving a variable a name. Notice that one function callled another.
        print "\n Then take the first move, you will need it."
        human = X
        computer = 0
    else:
        print "\n Your bravery will beyour undoing .... I will go first."
        computer = X
        human = O
    return computer, human

1 个答案:

答案 0 :(得分:2)

您需要定义并调用主函数

def main():
   display_instruct()
   #the rest of the code


if __name__ == "__main__":
    main()

您的代码没有运行的原因是因为您拥有的是函数定义

def func() ...

和价值分配

x=5

要实际运行某些东西,你需要定义一个main函数,它接受你定义的所有东西并以有意义的方式组合它们,或者附加到代码的底部,类似于你在main函数中写的东西