2016-11-16 58 views
1

我一直在Python中使用全局變量作爲一個小文本遊戲,並且遇到了很多文章,說全局變量在python中是不允許的。我一直在試圖理解如何讓我在下面得到(只是一個健康變量,能夠改變它並打印它)使用類,但我很困惑如何在類中轉換這樣的東西。任何幫助,例如,指向正確的方向將是偉大的。將全局變換爲類

這裏是我使用變量的一個例子。

import sys 
import time 

health = 100 
b = 1 

def intro(): 
    print("You will die after two moves") 


def exittro(): 
    time.sleep(1) 
    print("Thanks for playing!") 
    sys.exit() 


def move(): 
    global health 
    global b 
    health -= 50 

    if health <= 51 and b >0: 
     print("almost dead") 
     b = b - 1 


def death(): 
    if health == 0 or health <= 0: 
     print("...") 
     time.sleep(1) 
     print("You died\n") 
     time.sleep(2) 
     print("Dont worry, this game sucks anyway\n") 
     exittro() 

intro() 

a = 1 

while a == 1: 
    input("Press Enter to move") 
    move() 
    death() 

謝謝

編輯:這是那種我一直在努力做的事情......

class Test: 
def __init__(self): 
    number = 100 

def __call__(self): 
    return number 

def reduceNum(self): 
    number -=10 

def printNum(self): 
    print(number) 

a = 1 
while a == 1: 
    input("Enter") 
    Test.self.reduceNum() 
    Test.self.printNum() 
+0

你有什麼試過的?你有錯誤嗎?向我們展示你到目前爲止所擁有的。 –

+0

這樣的事情,它的許多適於變化'類測試: DEF __init __(個體): 數= 100 DEF __call __(個體): 返回數 DEF reduceNum(個體): 數 - = 10 DEF printNum(個體): 打印(數字) 一個= 1 而== 1: 輸入( 「輸入」) Test.self.reduceNum() Test.self.printNum()' – Anthony

+0

安東尼,將代碼添加到原始問題。 –

回答

3

我會避免這一類,如類一般都較慢。您可以使該函數返回health變量的新值。

我也建議製作一個主控制器函數來獲取返回值並將其應用於其他函數。這可以防止函數範圍之外的全局變量。

import time 

def intro(): 
    print("You will die after two moves") 


def outro(): 
    time.sleep(1) 
    print("Thanks for playing!") 
    # sys.exit() # You can avoid this now by just stopping the program normally 


def move(health): 
    health -= 50 

    if health <= 51: 
     print("almost dead") 
    return health # Return the new health to be stored in a variable 


def death(health): 
    if health <= 0: 
     print("...") 
     time.sleep(1) 
     print("You died\n") 
     time.sleep(2) 
     print("Dont worry, this game sucks anyway\n") 
     return True # Died 
    return False # Didn't die 

def main(): 
    health = 100 # You start with 100 health 
    intro() 
    while not death(health): 
     # While the death function doesn't return `True` (i.e., you didn't die) ... 
     input("Press enter to move") 
     health = move(health) # `health` is the new health value 
    outro() 

如果你想使用類,實際上你需要做instance = Test()類實例化(建立從一個新的對象)。您還需要將變量存儲爲自身的屬性(因此self.number = number),因爲任何局部變量都彼此不同。

class Test: 
    def __init__(self): 
     self.number = 100 

    def __call__(self): 
     return self.number 

    def reduceNum(self): 
     self.number -= 10 

    def printNum(self): 
     print(self.number) 

a = 1 
game = Test() 
while a == 1: 
    input("Enter") 
    game.reduceNum() 
    game.printNum() 
    # Or: 
    print(game()) 
    # As you've changed `__call__` to return the number as well. 
+0

非常感謝你,我之後是什麼,非常感謝 – Anthony

+0

爲什麼班級一般比較慢? – nycynik

+1

@nycynik Python內置函數,像'dict's和'set's是本地實現的(所以對於從python.org下載的常規Python,使用C),並進行大量的優化。類不能有太多的優化,因爲它們可以是任何東西,所以Python無法加速它們 – Artyer