
怎么修改这个代码,使他是以函数的方式调用,而且这个函数的参数:洞口数和抓取数(默认为10)
没搞懂你具体要干啥,不过挺好玩的
import random
day = 10
hole = [0, 0, 0, 0, 0]
foxp = random.randint(0, 4)
hole[foxp] = 1
def FM(hole):
move = random.randint(0, 1)
if move == 1 and hole[4] != 1:
old = hole.index(1)
hole[old] = 0
hole[old + 1] = 1
def game(day, hole):
x = 0
while x < day:
while True:
try:
playerNum = int(input('请输入你想打开洞的编号(1,2,3,4,5):')) - 1
if 0 <= playerNum <= 4:
break
except Exception:
print('输入错误,请重新输入!')
if hole[playerNum] == 1:
print('恭喜你在第{}天的{}号洞抓到了狐狸!'.format(x+1, playerNum+1))
break
else:
print('第', x+1 ,'天',playerNum+1,'号洞没有狐狸')
FM(hole)
x += 1
if x == day:
print('超过十天,失败了')
game(day, hole)