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

2024-09-28 01:25:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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))

我读过另一个类似的问题,说做“pile1=print(initGame())”将返回“initGame()”函数中的变量“chip”,但当我运行它时,它看起来是这样的

^{pr2}$

当它看起来像这样的时候

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

所以我的问题是如何让“chip”变量在我的另一个函数中工作?在

编辑:

 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)    

Tags: thetofromyouinputifdefprint
2条回答

使用print语句对initGame()函数进行嵌套调用,并假设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文件并将以下内容放入其中:

^{pr2}$

这是否符合预期?在

编辑2(回复新评论):

^{3}$

您不应该调用initGame()函数(我已经注释过了),因为它已经在displayPiles()内部调用过了。还应将其重命名为initPile()。在

打印返回的值为“无”,您需要存储值并单独打印,即:

而不是:

pile2= print(initGame()) 

尝试:

^{pr2}$

相关问题 更多 >

    热门问题