python使用函数

时间:2016-10-13 03:28:27

标签: python function variables

我正在尝试为我正在学习的基本python课程编写一个函数。我们正处于团队加入的过程中,并以团队形式开展一个项目。我已经指定每个成员将他们的部分写为函数,希望我可以调用每个函数来执行整个程序。我玩对象编程已经有一段时间了,这超出了课程要求,但我想尝试做这项工作。

我很难将变量传递给函数,然后从函数中检索更改的变量。

我曾尝试在多个网站上阅读并深入搜索,但我遗漏了一些内容,我们将非常感谢您的帮助。

以下是我尝试的第一个代码示例

import random
MOBhp=100

def ATTACK(TYPEatck,MOBhp):
#    global MOBhp

    print ('the type attack chosen was ',TYPEatck,'the MOB has ',MOBhp)
    if TYPEatck =='M'or TYPEatck =='m':
        print ('the ',PLAYER,' used melee to attack',MOB)
        MOBhp=MOBhp-10
    elif TYPEatck =='R'or TYPEatck =='r':
        print ('the ',PLAYER,' used range to attack',MOB)
        MOBhp=MOBhp-5
    else:
        print ('please choose a valid attack')
    print ('the MOB hitpoints are ',MOBhp)
    return MOBhp;


PLAYER='HERO'
MOB='Dragon'
AC=12
while MOBhp > 0:
   TYPEatck=random.choice('RM')
   ATTACK(TYPEatck,MOBhp)
print('really the MOB hitpoints are ', MOBhp)        
print(MOB,'was slain by ',PLAYER)

这给出了一个重复的结果,我必须打破cntrl + c

the type attack chosen was  R the MOB has  100
the  HERO  used range to attack Dragon
the MOB hitpoints are  95
the type attack chosen was  M the MOB has  100
the  HERO  used melee to attack Dragon
the MOB hitpoints are  90
the type attack chosen was  R the MOB has  100
the  HERO  used range to attack Dragon
the MOB hitpoints are  95

如果我执行以下操作

enter code here
#while MOBhp > 0:
TYPEatck=random.choice('RM')
ATTACK(TYPEatck,MOBhp)
print('really the MOB hitpoints are ', MOBhp)        
print(MOB,'was slain by ',PLAYER)

我得到以下结果

the type attack chosen was  R the MOB has  100
the  HERO  used range to attack Dragon
the MOB hitpoints are  95
really the MOB hitpoints are  100
Dragon was slain by  HERO

我也尝试过使用全局变量,似乎无法使用它。

3 个答案:

答案 0 :(得分:2)

就像现在一样,你丢掉了计算的结果。您必须将其用作下一次计算的输入。

while MOBhp > 0:
    TYPEatck=random.choice('RM')
    MOBhp = ATTACK(TYPEatck,MOBhp)

答案 1 :(得分:0)

MisterMiyagi的回答很有帮助。 如果要使用全局变量,可以像下面这样使用它:

import random
MOBhp=100

def ATTACK(TYPEatck, hp):
    global MOBhp
    MOBhp = hp

    print('the type attack chosen was ',TYPEatck,'the MOB has ',MOBhp)
    if TYPEatck == 'M' or TYPEatck == 'm':
        print('the ',PLAYER,' used melee to attack',MOB)
        MOBhp = MOBhp - 10
        elif TYPEatck == 'R' or TYPEatck == 'r':
        print('the ',PLAYER,' used range to attack',MOB)
        MOBhp = MOBhp - 5
    else:
        print('please choose a valid attack')
    print('the MOB hitpoints are ',MOBhp)
    return MOBhp;


PLAYER = 'HERO'
MOB = 'Dragon'
AC = 12
while MOBhp > 0:
    TYPEatck = random.choice('RM')
    ATTACK(TYPEatck,MOBhp)

print('really the MOB hitpoints are ', MOBhp)        
print(MOB,'was slain by ',PLAYER)

或者,你可以使用class:

import random

class Game():
    def __init__(self, player, mob):
        self.player = player
        self.mob = mob
        self.mobhp = 100

    def ATTACK(self, TYPEatck):
        print('the type attack chosen was ',TYPEatck,'the MOB has ',self.mobhp)
        if TYPEatck == 'M' or TYPEatck == 'm':
            print('the ',self.player,' used melee to attack',self.mob)
            self.mobhp -= 10
        elif TYPEatck == 'R' or TYPEatck == 'r':
            print('the ',self.player,' used range to attack',self.mob)
            self.mobhp -= 5
        else:
            print('please choose a valid attack')
        print('the MOB hitpoints are ',self.mobhp)
        return self.mobhp;

    def get_MOBhp(self):
        return self.mobhp

PLAYER = 'HERO'
MOB = 'Dragon'
AC = 12
game = Game(PLAYER, MOB)
while game.get_MOBhp() > 0:
    TYPEatck = random.choice('RM')
    game.ATTACK(TYPEatck)

print('really the MOB hitpoints are ', game.get_MOBhp())        
print(MOB,'was slain by ',PLAYER)

答案 2 :(得分:0)

谢谢大家的帮助!我想通了,能够写完我的程序。

Here is the code formatted and shared on Reddit