不能在Python中使用函数

2024-09-28 05:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用python做了一段时间的项目,我不能调用main函数。我找了一段时间,但找不到。现在的问题是我不能调用main函数。以前程序一旦通过主函数就不会返回主函数,但现在它甚至不会进入主函数,我不能从shell调用它。有人能帮我吗?这是我的密码。你知道吗

#!/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()

谢谢!你知道吗


Tags: andoftheto函数inyoumain
2条回答

Python没有main函数。先定义函数,让解释器知道它们。然后,它将在最后一个“return”之后执行第一个语句,然后执行后面的语句。你知道吗

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()

我看不出您最初在哪里调用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")

相关问题 更多 >

    热门问题