无法在Python中使用函数

时间:2016-02-26 04:10:18

标签: python function

我已经在python中开展了一段时间的项目,但我无法调用主要功能。我搜索了一会儿,但我无法上班。现在问题是我无法调用主函数。过去,一旦程序通过主功能一旦它不会返回主功能,但现在它甚至不会进入主功能,我无法调用它从壳。有人能帮助我吗?这是我的代码。

#!/usr/bin/python
# I only have the above part in case the
# end user does not have python

# Wizard's Quest Copyright (c) 2016 by GrayHat4Life
# This software is open source and may be distributed freely

# Prepare the program
import sys, random

# Set up some variables & begin the story
global health
health = 10
global enemiesKilled
enemiesKilled = 0
print("You are a wizard travelling through the kingdom")
print("Your quest: to save the kingdom by placing the Gem of Karn in its rightful place, the Tower of Souls")
# Main gameplay loop
def main():
# Battle code
# Set up the battle variables
    enemyList = ["Goblin", "Witch", "Dark Mage", "Dark Knight", "Theif", "Troll", "Orc"]
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
# Prepare the battles
    print("As you are walking through the Forest of Doom, a " + random.choice(enemyList) + " appears on the path in front of you!")
# This part isn't very usefull, it's only to give the
# player the illusion of free will. It's actually a kind of gambling game :s
    input("What spell do you use to attack it? ")
# The attack strengths
    attack = random.choice(numbers)
    enemyAttack = random.choice(numbers)
# Let the battle begin!
    if attack > enemyAttack:
        health = health - 1
        print("Your attack failed and you've been hit! You narrowly escape the enemy and continue your quest")
        main()
    elif enemyAttack > attack:
        enemiesKilled = enemiesKilled + 1

# This loop gives the user the ability to win. And lose. :D
while True:
    if enemiesKilled == 10:
        print("You've finally made it to the end of your quest!")
        print("You place the Gem of Karn in the soul sphere and save the kingdom!")
        sys.exit()
    elif health == 0:
        print("The damage was too much.")
        print("You bleed out in the middle of the forest, alone and friendless.")
        print("GAME OVER")
        sys.exit()

谢谢!

2 个答案:

答案 0 :(得分:0)

我最初看不到你在哪里调用main()方法。

目前,您输入的是while True:,条件enemiesKilled == 10:health == 0:都不是

如果您希望执行它,您的代码应遵循此结构

import somestuff

def main():
    # do something here

if __name__ == '__main__':
    # call the main function
    main()

如果你想要while True:件,那么我会推荐这样的东西来阻止无限循环

if __name__ == '__main__':
    # call the main function
    while enemiesKilled != 10 or health != 0:
        main()

    if enemiesKilled == 10:
        print("You've finally made it to the end of your quest!")
        print("You place the Gem of Karn in the soul sphere and save the kingdom!")
    elif health == 0:
        print("The damage was too much.")
        print("You bleed out in the middle of the forest, alone and friendless.")
    print("GAME OVER")

答案 1 :(得分:0)

Python没有主要功能。首先定义您的函数,以便解释器了解它们。然后它会在最后一次返回后执行第一个语句。之后的声明。

更正python的语法:

#!/usr/bin/python

# Function definition is here
def printme( str ):
  "This prints a passed string into this function"
  print str
  return;

# Now you can call printme function
printme()