Python多功能范围

时间:2015-12-04 21:04:21

标签: python function if-statement

我是python的新手并且已经阅读了python范围,但是我正在努力使用前一个函数的值,例如在这种情况下,如何获得在第二个函数中使用的随机生成的主场和客场目标?请记住,我刚开始学习python,所以我的代码根本不是最好的尝试。任何帮助将不胜感激,因为我找不到与我的情况相关的任何线程,谢谢。

class One():

    def Team(home_goals,away_goals):

        home_goals = ()
        away_goals = ()        

        home_goals = round(random.uniform(0,10.0)

        return home_goals

        away_goals = round(random.uniform(0.1,10.0)

        return away_goals 



    def winner(home_goals,away_goals):

        if home_goals > away_goals
              winner = ("home")

        elif home_goals == away goals 
              winner = ("draw")

        else:
              winner = ("away")

        print(winner)

        return winner

2 个答案:

答案 0 :(得分:1)

只需将第一个函数的结果传递给第二个函数:

def score():
    home_goals = 2
    away_goals = 3
    return home_goals, away_goals

def winner(home, away):
    # stuff ...
    pass

home, away = score()
winning_team = winner(home, away)

答案 1 :(得分:0)

如果您只是寻找直接功能,Bi Rico有正确的答案。但是,你提到范围,你的代码看起来像是在尝试成为一个类。所以让我们建立一个合适的班级。

在类中,函数被称为方法。这是一种花哨的说法,“这些功能的范围是在'One'类中。

import random

class One(object):
    home_goals = ()
    away_goals = ()

    def playGame(self):  
        self.home_goals = round(random.uniform(0,10.0))
        self.away_goals = round(random.uniform(0.1,10.0))
        return self.home_goals, self.away_goals 


    def determineWinner(self):
        if not self.home_goals or not self.away_goals:
            print("Teams must play game first!")
            return None
        if self.home_goals > self.away_goals:
            winner = ("home")
        elif self.home_goals == self.away_goals: 
            winner = ("draw")
        else:
            winner = ("away")
        print(winner)
        return winner

## usage 
one_obj = One() #create instance of One()
home, away = one_obj.playGame() 
game1 = one_obj.determineWinner()

one_obj.playGame() #play a new game
game2 = one_obj.determineWinner()

类有一个名为'self'的特殊变量,它是对象one_obj在两个方法之间共享变量home_goalsaway_goals的方式。

如果我们不使用self,我们需要保存一个方法的输出并将它们传递给另一个方法。

您可以使用名为@staticmethod

的装饰器执行此操作
import random
# still using a class but without self

class Two(object):
    @staticmethod
    def playGame():
        home_goals = round(random.uniform(0, 10.0))
        away_goals = round(random.uniform(0.1, 10.0))
        return home_goals, away_goals

    @staticmethod
    def determineWinner(home_goals, away_goals):
        if not home_goals or not away_goals:
            print("Teams must play game first!")
            return None
        if home_goals > away_goals:
            winner = ("home")
        elif home_goals == away_goals:
            winner = ("draw")
        else:
            winner = ("away")
        print(winner)
        return winner

two_obj = Two()

home, away = two_obj.playGame()
game1 = two_obj.determineWinner(home, away)

类的要点基本上被绕过,只是一个容纳两个函数的对象。但是在您的问题的上下文中,您必须引用实例two_obj以调用这些方法。

在Bi Rico的例子中,函数在GLOBAL范围内。在上面的例子中,方法(也就是类中的函数)都在类的范围内。

可以在不实例化类Two()的情况下引用静态方法:

home, away = Two.playGame()
game1 = Two.determineWinner(home, away)