2016-02-26 27 views
0

使用功能,我一直工作在一個項目中的Python,而現在,我不能打電話的主要功能。我搜索了一段時間,但我無法實現它的工作。現在問題是我不能調用主函數。它曾經是,一旦該方案通過主功能去了一次就不會回去的主要功能,但現在它甚至不會進入的主要功能,我不能從外殼調用它。有人能幫助我嗎?這是我的代碼。不能在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() 

謝謝!

+0

你永遠不叫'主()'函數。它不會自動調用。 – Barmar

+0

我試圖調用它一出來就和語法錯誤想出了(顯然)前。我想我已經通過Stack Overflow的幫助解決了這個錯誤。 – GrayHat4Life

回答

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") 
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() 
+0

Python也不需要尾隨分號或隱式返回語句 –