所以我有这个游戏:
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”,我想进入 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
方法中。
再次强调,请确保在使用前声明。
关于python - 如何让一个函数转到另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901405/