Python 如何重用用户&x27;s在其他函数中的原始输入
我正在为PythonforHW制作一个2D棋盘游戏 我要求用户为电路板大小输入一个整数。例如,7。我已经修改了一点(只显示重要的)之前发布。函数如下所示Python 如何重用用户&x27;s在其他函数中的原始输入,python,python-2.7,Python,Python 2.7,我正在为PythonforHW制作一个2D棋盘游戏 我要求用户为电路板大小输入一个整数。例如,7。我已经修改了一点(只显示重要的)之前发布。函数如下所示 def asksize(): while True: ask=raw_input("Input board size: ") try: size=int(ask) return size except ValueE
def asksize():
while True:
ask=raw_input("Input board size: ")
try:
size=int(ask)
return size
except ValueError:
print "Please enter a integer"
因为它是可变板尺寸,我需要在其他函数中重用可变尺寸,使用它检查用户的移动是否有效,如何重用该变量
def checkmove(move):
#move is sth like eg. A1:B2
move=move.split(":") #I split it so it becomes ['A','1']['B','2']
if size>=int(move[0][1]) and int(move[0][1])>=1 and size>=int(move[1][1]) and int(move[1][1])>=1: #for example if board size is 7, this is to check whether user input is between 1 to 7 within the board
return True
else:
return False
在我的checkmove函数中,我不能在参数中使用size,因为它没有定义,我如何使它可行
谢谢你(至少)有两个选择:
checkmove
函数中将size
作为参数传递checkmove
函数中调用size=asksize()
def checkmove(move, size):
# rest of definition
# main bit of code
size = asksize()
checkmove(move, size)
使变量大小全局是一个选项,但是你应该考虑你的函数作为API,并在游戏主体BULL上使用它们。 所以像这样存储输入:
无论如何,这是一个糟糕的练习,最好通过游戏代码中的函数传递变量:#定义
def asksize():
#你的代码在这里
def checkmove(移动,大小):
#你的代码在这里
#游戏设置在这里
大小=asksize()
#更多设置的东西
#游戏结束设置
#游戏主代码
虽然是真的:#主要的游戏是口腔
#这里有游戏
选中移动(移动,大小)
#更多游戏内容
考虑创建一个类来表示电路板。那么大小自然是一个实例变量
class Board(object):
def asksize(self):
while True:
ask=raw_input("Input board size: ")
try:
self.size=int(ask)
return
except ValueError:
print "Please enter a integer"
def checkmove(self, move):
#move is sth like eg. A1:B2
move=move.split(":") #I split it so it becomes ['A','1']['B','2']
if self.size>=int(move[0][1]) and int(move[0][1])>=1 and self.size>=int(move[1][1]) and int(move[1][1])>=1: #for example if board size is 7, this is to check whether user input is between 1 to 7 within the board
return True
else:
return False
# Use it like this
board = Board()
board.asksize()
move = some_function_that_returns_a_move()
board.checkmove(move)
或者简单地将“size”作为全局变量,无论如何python将在下一个更高的范围内搜索该变量。您可以这样尝试:
size = 0
def asksize():
global size
while True:
ask=raw_input("Input board size: ")
try:
size=int(ask)
return size
except ValueError:
print "Please enter a integer"
def checkmove(move):
global size
# move is sth like eg. A1:B2
move = move.split(":") # I split it so it becomes ['A','1']['B','2']
if size >= int(move[0][1]) and int(move[0][1]) >= 1 and size >= int(move[1][1]) and int(move[1][
1]) >= 1: # for example if board size is 7, this is to check whether user input is between 1 to 7 within the board
return True
else:
return False
我试过你的(2)。结果是,它要求用户再次输入board size,但不返回size@user1687703是的,是的(询问功能中的大小)!如果没有完整的代码,就不可能知道是否需要这样做。1.无论如何,这是更好的选择。:)我不太确定在传递了两个参数后如何得到它,你能解释一下吗?
size = 0
def asksize():
global size
while True:
ask=raw_input("Input board size: ")
try:
size=int(ask)
return size
except ValueError:
print "Please enter a integer"
def checkmove(move):
global size
# move is sth like eg. A1:B2
move = move.split(":") # I split it so it becomes ['A','1']['B','2']
if size >= int(move[0][1]) and int(move[0][1]) >= 1 and size >= int(move[1][1]) and int(move[1][
1]) >= 1: # for example if board size is 7, this is to check whether user input is between 1 to 7 within the board
return True
else:
return False