python 战舰_战舰python代码学院
出于某種原因,我用來檢查猜測位置和實際位置是否相同的if語句就是不起作用。我已經(jīng)安排好了,所以我知道船在哪里。我試著讓它打印出所有的變量和它們各自的類型,就在我輸入猜測之后,在它檢查它們是否相等之前,但是由于某種原因,它繼續(xù)顯示我錯過了。在
但不管怎樣,它在這里:from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col
##########################################
#Value error checking and guess row inputs
##########################################
def get_guess_row():
guess_row = int(raw_input("Guess Row:"))
return guess_row
def value_guess_row():
try:
y = get_guess_row()
except ValueError:
print ("Please enter a valid number.")
y = value_guess_row()
return y
##########################################
#Value error checking and guess col inputs
##########################################
def get_guess_col():
guess_col = int(raw_input("Guess Col:"))
return guess_col
def value_guess_col():
try:
x = get_guess_col()
except ValueError:
print ("Please enter a valid number.")
x = value_guess_col()
return x
###############################
#Get the coloumn and row values
###############################
guess_col = value_guess_col()
guess_row = value_guess_row()
guess_col -= 1
guess_row -= 1
print ("Guessed coloumn: ", guess_col, "the type is: ", (type(guess_col)))
print ("Guessed row: ", guess_row, "the type is: ", (type(guess_row)))
print ("Actual coloumn: ", ship_row, "the type is: ", (type(ship_row)))
print ("Actual row: ", ship_col, "the type is: ", (type(ship_col)))
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sank my battleship!")
elif guess_row not in range(5) or \
guess_col not in range(5):
print ("Oops, that's not even in the ocean.")
elif board[guess_row][guess_col] == "X":
print ("You guessed that one already.")
else:
print ("You missed my battleship!")
board[guess_row][guess_col] = "X"
print_board(board)
總結
以上是生活随笔為你收集整理的python 战舰_战舰python代码学院的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纺织品有害物质三项检测
- 下一篇: 从网线到网页之物理层一网线网口