所以我试图使用嵌套列表制作扫雷车,并且我需要使用以逗号分隔的输入,以检查“ 3,4”处是否存在炸弹本身
为此,我的函数需要一个嵌套列表,并且应该采用两个代表行和列的整数。
虽然我在做什么:
def isMineAt(gameBoard, guess1, guess2) :
guess = input(("Enter a number for the row, then a number for the column"))
mainList = []
for i in range (0, int(guess)) :
mainList = guess.split(',')
print(mainList)
然后在main()中,我有:
isMineAt(gameBoard, '', '')
我得到的是一个错误,指出:以10为底的int()的无效文字:'3,4'
任何人都可以对发生的事情有所了解吗?和
答案 0 :(得分:1)
使用:
def isMineAt(gameboard, guess1, guess2) :
guess = str(input(("Enter a number for the row, then a number for the column")))
mainList = list(map(int,guess.split(',')))
print(mainList)
isMineAt(gameboard, '', '')
答案 1 :(得分:1)
程序需要正确处理用户输入,并且可能需要进行更好的错误检查。
用户正在输入一些字符串“ 3,2”,但也可能输入“ 3 2”或“ bananas!”。
所以首先考虑简单的情况:
try:
row,col = guess.split(",")
row = int(row)
col = int(col)
except:
print("Give input as integers: row,column")
未使用参数guess1
和guess2
。
因此,整个脚本可能应该分为两个功能:
### Prompt the user for some input, where they should enter
### Two comma separated integers.
### Return the inputted numbers to ints, returning row, col.
def getUserGuess():
row, col = (-1, -1)
while row == -1:
user_input = input("Give row & column numbers> ")
try:
row,col = guess.split(",")
row = int(row)
col = int(col)
except:
print("Error in input: expected Number,Number")
row = -1 # go around the loop again
return row, col
### Return True if the board has a mine at (row, col)
def isMineAt(board, row, col):
# TODO - verify row & col are within the bounds of the board
return board[row][col] == True # probably not a useful function