python在另一个函数中使用其他函数的变量_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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值