2013-04-09 82 views
2

所以我有这个游戏:如何让功能转到另一个功能?

import sys 

def Start(): 
    print("Hello and welcome to my first Python game. I made it for fun and because I am pretty bored right now.") 
    print("I hope you enjoy my EPIC TEXT GAME") 

play = input("do you want to play? (Y/N) ") 

if play == "Y": 
    game() 

if play == "N": 
    sys.exit() 

def game(): 
    print("That's pretty much what I've done so far! :P -- yea yea, it's nothing -- IT IS!. Bye now") 
    input("Press enter to exit") 

如果键入“Y”我想去游戏()。它没有。

+5

出于好奇,你perhpas编写“Y”(小写'y'?) – Torxed 2013-04-09 12:07:10

+0

另外,Python版本是非常重要的,我假设Python3.X .. – Torxed 2013-04-09 12:08:28

+1

假设它是python 3因为打印功能,所以在这里输入()是好的 – wim 2013-04-09 12:08:56

回答

4

您在尝试并使用它之后定义了game。在使用它们之前,您需要定义函数,变量等。 此外,您的代码仅匹配大写Y而不是小写y。为了让所有的输入大写字母,你应该使用.upper()方法就可以了
变化代码:

def game(): 
    print("That's pretty much what I've done so far!") 
    input("Press enter to exit") 

if play.upper() == "Y": 
    game() 
elif play.upper() == "N": 
    sys.exit() 

而且它通常是最好的形式没有任何全球性的代码,而是包含在一个main功能,如果Python代码正在作为主要代码运行。你可以通过使用:

if __name__ == "__main__": 
    Start() 

然后把所有全局代码放入Start方法中。 再次确保您在使用之前声明。

+0

工程就像一个魅力!非常感谢您的帮助。 – user2261574 2013-04-09 12:09:56

+0

没问题。你应该考虑看看[Learn Python The Hard Way](http://learnpythonthehardway.org/book/)以获得所有这些多汁的知识。 – Serdalis 2013-04-09 12:11:10

-1

您正在使用input()函数,该函数在从标准输入读取后立即将输入作为命令执行。

你可能想使用raw_input()将简单地返回什么用户输入

+0

这是python 3.0,它没有'raw_input'(你可以通过print语句来判断)。 – Serdalis 2013-04-09 12:14:46

+0

打印功能也在Python 2.7中有效。我不知道3.x中的输入/ raw_input变化。谢谢。 – BradS 2013-04-10 00:51:08