我如何确保输入一个数字会得到一个随机的结果如清单所示?

2024-09-30 06:15:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我为一个班级开发的游戏中做一个场景。在这个场景中,玩家会找到三个随机的房间,其中一个房间有他们需要的钥匙。然而,每当我测试代码看它是否工作,而不是把播放器放在一个房间,代码“倒回”到一个较早的决定,并从那里去

我尝试重新定位输入、随机和不同的函数,以使其按预期工作,但没有效果。 例如,我尝试将input()放在while循环之前,还尝试定义其他函数作为input()的路径

def checkRoom():
    room = input()
    return room
    while room != '1' or room != '2' or room != '3':
        room1, room2, room3 = random.sample(range(1, 4), 3)
        if room == str(room1):
            print('You enter a room.')
            time.sleep(1)
            print('''The room looks like a replica of your dungeon room,
    but nothing else is unusual about the room. You leave the
    room.''')
            time.sleep(5)
            checkRoom()
        elif room == str(room2):
            print('You enter a room.')
            time.sleep(1)
            print('''The room appears, weirdly, as a leisure library. 
Every book in the library is a different novel; not a spell book
in sight. While you'd love to sit and enjoy a good book,
you want out of this castle as soon as possible. You leave
the room.''')
            time.sleep(6)
            checkRoom()
        elif room == str(room3):
            print('You enter a room.')
            time.sleep(1)
            print('''You see the room is completely empty, save for a 
single nightstand. You go over to it and open it, revealing a
generic-looking key. You take the key and walk over to
the door to the castle, placing the key in the keyhole
and turning it, eliciting a "Click!" from the door as
it opens to the outside.''')
            time.sleep(8)
            print('You escaped! Now to destroy the castle.')
            time.sleep(3)
            endgame()

应该发生的是玩家进入一个随机的房间,但最终是玩家需要离开城堡的房间。实际上,播放器被“倒回”到先前的输入,代码跳回这些输入并从那里运行


Tags: andthetoyouinputtimeas玩家

热门问题