如何延迟函数内可用的选项?

时间:2017-04-30 13:34:56

标签: python

我有一个地牢爬虫游戏,我正在使用该功能决定玩家进入哪个房间我希望能够延迟退出房间,直到玩家进入一定数量的房间。这是功能:

def room_enter(empty_room, ghost_room, monster_room, exit_room, crumbly_room, locked_room, surgery_room, prisoner_encounter):
    global player_health
    global room_count
    security = True
    while security == True:
        direction = input("\nYou have the option to go Left (l), Forward (f), or Right (r), which direction would you like to go? ")
        print("\n ")
        if direction == "l" or direction == "f" or direction == "r":
            security = False
            room_no = random.randint(1,16)

            if room_no == 1 or room_no == 2 or room_no == 3:
                empty_room()
                room_count = room_count + 1
            if room_no == 4 or room_no == 5:
                ghost_room()
                room_count = room_count + 1
            if room_no == 6:
                monster_room()
                room_count = room_count + 1
            if room_no == 7 or room_no == 16:
                exit_room()
                room_count = room_count + 1
            if room_no == 8 or room_no == 9:
                crumbly_room()
                room_count = room_count + 1
            if room_no == 10 or room_no == 11:
                locked_room()
                room_count = room_count + 1
            if room_no == 12 or room_no == 13:
                surgery_room()
                room_count = room_count + 1
            if room_no == 14 or room_no == 15:
                prisoner_encounter()
                room_count = room_count +1
        else:
            print("\nInvalid Entry")

2 个答案:

答案 0 :(得分:0)

您可以使用in测试多个选项:

if direction in "lfr":

您真的不需要security变量,因为当您获得有效的break时,您只能input

while True:
    if direction in "lfr":
        ...
        break

假设你只需要一个empty_room()而不是exit_room()如果room_count < certain_number_room那么你需要额外的警卫:

if room_no in {7, 16}:
    if room_count < 5:
        empty_room()
    else:
        exit_room()

鉴于您使用每个选项递增room_count,只需将其移到if语句之外:

    if direction in "lfr":
        ...
        room_count += 1
        break

就个人而言,如果支票很短,我更喜欢测试负面和continue vs有一个大的缩进块。总而言之,你得到:

while True:
    direction = input("\nYou have the option to go Left (l), Forward (f), or Right (r), which direction would you like to go? ")
    print("\n ")
    if direction not in "lfr":
        print("\nInvalid Entry")
        continue

    room_no = random.randint(1,16)

    if room_no in {1, 2, 3}:
        empty_room()
    elif room_no in {4, 5}:
        ghost_room()
    elif room_no in {6}:
        monster_room()
    elif room_no in {7, 16}:
        if room_count < 5:
            empty_room()
        else:
            exit_room()
    elif room_no in {8, 9}:
        crumbly_room()
    elif room_no in {10, 11}:
        locked_room()
    elif room_no in {12, 13}:
        surgery_room()
    elif room_no in {14, 15:
        prisoner_encounter()
    room_count += 1
    break

我希望避免使用global变量,也许会看一下class的封装。

答案 1 :(得分:0)

我会以OOP的方式重写整个事情。你的代码只有太多的逻辑分支可以跟随它变得无法管理且对错误友好&#34;。使用类属性来保持状态,你就可以了。

此外,使用全局关键字会使您的代码面临开发人员无法控制问题的风险。