如何将变量从一个函数拉到另一个函数

时间:2016-11-13 11:36:32

标签: python

    def letterChoice():
        playerLetter = input('Please choose X or O.').upper()
        if playerLetter in ['X','O']:
          print('The game will now begin.')
        while playerLetter not in ['X','O']:
          playerLetter = input('Choose X or O.').upper()
        if playerLetter == 'X':
          computerLetter = 'O'
        else:
          computerLetter = 'X'
        turnChooser()
    def turnChooser():
        choice = input("Would you like to go first, second or decide by coin toss?(enter 1, 2 or c) ")
        while choice not in ["1","2","c"]:
          choice = input("Please enter 1, 2 or c. ")
        if choice == 1:
          print("G")
          cur_turn = letterChoice.playerLetter()
        elif choice == 2:
          print("H")
        else:
          print("P")
        moveTaker()

我无法弄清楚我应该如何将playerLetter继承到turnChooser(),我已经尝试将playerLetter放入每个函数的括号中但是它们不会传递和创建一个参数错误和print("G")等只是在那里查看代码是否有效,但只要我输入1或2" P"输出。

4 个答案:

答案 0 :(得分:0)

您应该尝试使用类:Python documentation

这应该是代码:

class Game:

    def __init__(self):
        self.cur_turn = ''
        self.choise = ''
        self.playerLetter = ''
        self.computerLetter = ''

    def letterChoice(self):
        while True:
            self.playerLetter = input('Please choose X or O.').upper()
            if self.playerLetter in ['X','O']:
                print('The game will now begin.')
                if playerLetter == 'X':
                   self.computerLetter = 'O'
                else:
                   self.computerLetter = 'X'
                break
            else:
                print ('Please enter only X or O')      

     def turnChooser(self):
         while True:
             self.choice = input("Would you like to go first, second or decide by coin toss? (enter 1, 2 or c) ")
             if self.choice in ["1","2","c"]:
                 if self.choice == 1:
                   print("G")
                   self.cur_turn = self.playerLetter()
                 elif self.choice == 2:
                   print("H")
                 else:
                   print("P")
                 break
             else:
                 print ('Please enter 1, 2 or c')

game = Game()
game.letterChoice()
game.turnChooser()

# If you want to read any of the variables in Game just write 'self.VARIABLE_NAME'

答案 1 :(得分:0)

您需要为playerLatter

定义函数属性

对于EX:

def foo():
    foo.playerletter=input('Please choose X or O.').upper()


>>> foo()
Please choose X or O.x

>>> foo.playerLetter
'X'

从其他功能访问

def bar():
    variable=foo.playerLetter
    print(variable)


>>> bar()
X
>>> 

您始终可以查看给定功能可用的属性

>>> [i for i in dir(foo) if not i.startswith('_')]
['playerLetter']
>>> 

答案 2 :(得分:0)

将turnchooser()编辑为turnchooser(var),然后在调用函数时将字母传递给函数,如下所示:

def LetterChoice():

   Code...

   turnchooser(playerletter)

def turnchooser(var):
       Code...

这封信将放在一个名为var的变量中,这意味着你的代码将使用该字母作为var not playerletter。

当然,您可以将名称更改为您喜欢的名称。

你可以为函数添加尽可能多的变量,但是它们都应该为它们分配一些东西,也就是说你不能这样调用前面的函数:

turnchooser()

除非您为其指定默认值:

def turnchooser(var = 'x')

这种方式只要函数调用" var"除非另有说明,否则为x。

请注意,如果要将其从一个函数传递给另一个函数,则必须将该字母分配给变量,然后在" LetterChoice"之外调用该函数。或者在#34; LetterChoice"

的定义中称呼它

答案 3 :(得分:0)

在具有变量的函数中输入:

global variableName

显然将variableName更改为实际调用的变量。希望这有帮助!

托米