python的抓狐狸游戏的修改

img

怎么修改这个代码,使他是以函数的方式调用,而且这个函数的参数:洞口数和抓取数(默认为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('请输入你想打开洞的编号(12345):')) - 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)

给贴个原始代码呗

img

游戏的本质是N次内能否猜中电脑在洞数范围内的随机数字,而且数字每次都可能变。
你这搞得有点复杂了,而且代码里出现一个未定义的列表holeList,不知道是不是漏了,还是贴错了。
试试这个效果是不是一样:

import random

def guess_fox(day,hole):
    for i in range(day):
        fox = random.randint(1,hole)
        playerNum = int(input('请输入你想打开洞的编号:'))
        if playerNum == fox:
            print(f'恭喜你在第{i+1}天的{playerNum}号洞抓到了狐狸!')
            return
        else:
            print(f'{playerNum}号洞没有狐狸')
    print(f'超过{day}天,失败了')

if __name__ == '__main__':
    guess_fox(int(input('请输入天数:')),int(input('请输入洞口数:')))