瘋狂Python講義Unit4 gobang

2021-10-21 18:09:48 字數 1169 閱讀 1443

#!/usr/bin/python3

'''命令列五子棋

'''import random

board_size=15

board =

# 初始化棋盤

def initboard():

for i in range(board_size):

# 畫棋盤

def scripboard():

for i in range(board_size):

for j in range(board_size):

print(board[i][j],end=' ')

print()

# 電腦隨機棋子

def randomscript():

while true:

randx, randy = random.randint(0, 14), random.randint(0, 14)

if board[randx][randy] == '●' or board[randx][randy] == '○':

continue

else:

board[randx][randy] = '○'

return

initboard()

scripboard()

input_xy=input('請輸入棋子座標,以逗號分割,如1,2\n')

while input_xy != none:

x, y = input_xy.split(',')

if board[int(x)-1][int(y)-1] == '●' or board[int(x)-1][int(y)-1] == '○':

scripboard()

print('輸入重複,請重新輸入')

input_xy=input('請輸入棋子座標,以逗號分割,如1,2\n')

continue

else:

board[int(x) - 1][int(y) - 1] = '●'

scripboard()

randomscript()

print('電腦隨機落子結束,進入下一回合')

scripboard()

input_xy = input('請輸入棋子座標,以逗號分割,如1,2\n')

瘋狂Python講義Unit4 數字轉人民幣讀法

usr bin python3 num transfer to rmb 借鑑了講義中4位的數字字串轉換成中文的函式 優化了小數部分和0的處理,仍舊不夠完善 分離整數和小數部分 def divide num inttemp int num fractiontemp round num inttemp ...

Unit4如何使用類

建立物件,呼叫方法 public class test 屬性有預設值,預設值規則 整數和字元型預設位0,小數預設為0.0,布林型別預設為false,引用型別預設為null 使用預設構造方法建立物件 jvm預設構造方法 public class person 等價於以下 public class pe...

《瘋狂Python講義》之異常處理

異常機制已經成為衡量一門程式語言是否成熟的標準之一,使用異常處理機制的python程式有更好的容錯性,更加健康。python的異常處理機制可以讓程式具有極好的容錯性。使用try except捕獲異常 語法結構如下 try 業務實現 except error1,error2,as e alert 輸入...