Python:如何在另一个函数中使用函数中的变量

时间:2014-05-05 05:55:01

标签: python function variables

我在python中创建一个游戏,所以这就是我得到的......

def initGame():
    chip = int(input("How many chips would you like to start with? "))
    if chip<=0:
       print("Please input a number greater than 0")

    return chip
def displayPiles():
    print("It is your turn human.")
    print("Here are the piles:")
    pile1= print(chip)                     #What I originally had
    pile2= print(initGame())               #What I did after 
    print("pile 1: ", str(pile1))
    print("pile 2: ", str(pile2))

我读了另一个类似的问题,说做&#34; pile1 = print(initGame())&#34;将返回变量&#34; chip&#34;在&#34; initGame()&#34;功能,但当我运行它看起来像这样

Welcome to the game of chips. I know you know the rules so let's go.
How many chips would you like to start with? 12
It is your turn human.
Here are the piles:
How many chips would you like to start with? 

什么时候应该看起来像这样

Welcome to the game of chips. I know you know the rules so let's go.
How many chips would you like to start with? 12
It is your turn human.
Here are the piles:
pile 1: 12
pile 2: 12

所以我的问题是如何获得&#34;芯片&#34;变量在我的其他函数中工作?

编辑:

 def initGame():
    chip = int(input("How many chips would you like to start with? "))
    if chip<=0:
        print("Please input a number greater than 0")

    return chip
def displayPiles():
    #chip= (trying to define chip from the initGame function so that I can set it to pile1,pile2)
    pile1= initGame()
    pile2= pile1
    print("It is your turn human.")
    print("Here are the piles: ")
    print("pile 1: "+ str(pile1))
    print("pile 2: "+ str(pile2))

    return pile1, pile2
def getHumanMove():
    x=int(input("Which pile would you like to take from?(1 or 2)"))
    y=int(input("How many would you like from pile "+ str(x)+ "? "))
    #if y>chip: (trying to recall chip from initGame) 
        print("pile " +str(x)+ " does not have that many chips. Try again.")
    elif y==0:
        print("You must take at least one chip. Try again.")
    else:
        print("That was a legal move. Thank You.")
    print("Here are the piles: ")
    pile1= initGame()
    pile2= initGame()
    if x == 1:
        pile1= initGame()- y
        print("pile 1: ", str(pile1))
        print("pile 2: ", str(pile2))
    elif x == 2:
        pile2= initGame()- y
        print("pile 1: ", str(pile1))
        print("pile 2: ", str(pile2))
        return pile1, pile2

def getCompMove(getHumanMove):
    print("Now it's my turn.")
    #x= (Trying to recall the x,y values from the function getHumanMove)
    #y= 
    finished = False
    while not finished:
        if x==1:
            print("I, the champion chips computer will take "+str(y)+ " chips from pile 2")
            pile2= pile2 - y
        elif x==2:
            print("I, the champion chips computer will take "+str(y)+ " chips from pile 1")
            pile1= pile1 - y
        if pile1==0 and pile2==0:
            finished= True
            print("The game is over because I took the last chip.")
            print("Thanks for playing. Let's wager next time.")        
    return pile1,pile2


#######################################################################
#Main

print("Welcome to the game of chips. I know you know the rules so let's go.")

#chips= initGame()

piles = displayPiles()

move= getHumanMove()

comp= getCompMove()

print(move)    

2 个答案:

答案 0 :(得分:2)

你正在使用一个带有嵌套调用initGame()函数的print语句,你假设print()将返回变量pile1和pile2。

试试这个:

def initGame():
    chip = int(input("How many chips would you like to start with? "))
    if chip<=0:
       print("Please input a number greater than 0")

    return chip

def displayPiles():
    print("It is your turn human.")
    print("Here are the piles:")
    pile2 = initGame()                  # Changed here
    print("pile 2: ", str(pile2))

编辑#1(回复评论):

为了测试这部分代码(仅限),创建一个新的.py文件并将以下内容放入其中:

#!/usr/bin/python

def initGame():
    chip = int(input("How many chips would you like to start with? "))
    if chip<=0:
       print("Please input a number greater than 0")

    return chip

def displayPiles():
    print("It is your turn human.")
    print("Here are the piles:")
    pile2 = initGame()                  # Changed here
    print "pile 2:", str(pile2)

displayPiles()

这是否符合预期?

编辑#2(回复新评论):

def initGame():
    chip = int(input("How many chips would you like to start with? "))
    if chip<=0:
        print("Please input a number greater than 0")

    return chip
def displayPiles():
    print("It is your turn human.")
    print("Here are the piles: ")
    pile1= initGame()
    pile2= initGame()
    print("pile 1: " + str(pile1))       # Changed here
    print("pile 2: " + str(pile2))       # Changed here

    return pile1, pile2

def getHumanMove():
    x=int(input("Which pile would you like to take from?(1 or 2)"))
    y=int(input("How many would you like from pile " + str(x) + "? "))   # Changed here

    return x,y


#######################################################################
#Main

print("Welcome to the game of chips. I know you know the rules so let's go.")

#chips= initGame()            # Commented this

piles = displayPiles()

move= getHumanMove()

print move                    # Added this

你不应该调用initGame()函数(我评论过的函数),因为它已在displayPiles()中调用。您还应该将其重命名为initPile()。

答案 1 :(得分:2)

印刷品的退货是无您需要存储价值并单独打印,即:

而不是:

pile2= print(initGame()) 

尝试:

pile2 = initGame()
print(pile2)