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