import random import sys import numpy as np def draw_map(board, length): for i in range(length): print('====',end='') print('') for i in range(length): for j in range(length): print('| ' + str(board[i, j]) + ' ',end='') print('') for i in range(length): print('====',end='') print('') def init(board, length, ore_num, rock_num): board[0, 0] = 8 #8是存放矿的地方 board[length-1, 0] = 9 #9是充电的地方 for i in range(ore_num): #生成矿物 x = random.randint(0, length-1) y = random.randint(0, length-1) if board[x,y] == 0: board[x,y] = 1 #1是矿 else: i -= 1 for i in range(rock_num): #生成障碍物 x = random.randint(0,length-1) y = random.randint(0,length-1) if board[x,y] == 0: board[x,y] = 2 #2是岩石 else: i -= 1 def main(): length = 5 # 长宽 ore_num = 5 # 矿物数量 rock_num = 5 # 岩石数量 board = np.zeros((length, length)) init(board, length, ore_num, rock_num) draw_map(board,length) if __name__ == '__main__': main() 我想让打印的数字没有小数点,应该怎么改
时间: 2024-01-03 16:05:34 浏览: 60
import random.py
你可以在print语句中使用int()函数将浮点数强制转换为整数,这样就可以去掉小数点了。修改draw_map函数中的print语句,将str(board[i, j])改为str(int(board[i, j]))即可实现:
```
def draw_map(board, length):
for i in range(length):
print('====',end='')
print('')
for i in range(length):
for j in range(length):
print('| ' + str(int(board[i, j])) + ' ',end='')
print('')
for i in range(length):
print('====',end='')
print('')
```
这样就可以打印没有小数点的整数了。
阅读全文