代码之家  ›  专栏  ›  技术社区  ›  John Conor

无限循环-While True[Python]

  •  0
  • John Conor  · 技术社区  · 5 年前

    陷入无限循环这个古老的问题。。。

    写一个文字冒险游戏,并把游戏机制写下来;地图和周围的运动。当我尝试实现其他功能时,遇到了一个无限循环的问题-获取项目等。

    下面是两段代码,包括初始化变量、移动代码和下一阶段的内容。在“…”下面是问题所在。

    所有压痕均对齐,如图所示。

    
    print("Welcome to this most intrepid" + game['name']
    + " your goal; should you dare to continue... is to" + game['goal'])
    star_loc = int(game['start'])
    curr_loc = star_loc 
    character = {'inventory': [], 'location':curr_loc}
    x_loc = 1
    y_loc = 2
    command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
    
    while True:
        command_parts = command.split()
        cmd = command_parts[0]
        print(cmd)
        obj = command_parts[-1]
        print(obj)
        #move around map
        if cmd == 'move' or cmd == 'go' or 'hiddenpath':
            #handles movement on the X plane - East, West
            elif obj == 'east':
                if x_loc == int(game['xsize']): # use the x size the game dict instead, compute the x and y 
                    curr_loc = curr_loc - 2
                    x_loc = 1
                    y_loc = y_loc
                    command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
                else:
                    curr_loc = curr_loc + 1
                    x_loc = x_loc + 1
                    y_loc = y_loc
                    command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
            elif obj == 'west':
                if x_loc == int(game['xsize']) - 2:
                    curr_loc = curr_loc + 2
                    x_loc = x_loc = 3
                    y_loc = y_loc
                    command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
                else:
                    curr_loc = curr_loc - 1
                    x_loc = x_loc - 1
                    y_loc = y_loc
                    command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
    
    .................
    
        if cmd == 'exit':
            print('Goodbye')
            break
    
        if cmd == 'inv':
            print('You are carrying')
            for item in character['inventory']:
                print('', item)
                continue 
    
        if cmd == 'goal':
            print(game['goal'])
        else:
            continue
        
        if cmd == 'take':
            if obj in map[curr_loc]['obj']:
                print("You take the " + str(map[curr_loc]['obj'][0]))
                map[curr_loc]['obj'].remove(obj)
                character['inventory'].append(obj)
            continue
    
    

    主要阻力。。。└[∵┌]└[ ∵ ]┘[┐∵]┘

    2 回复  |  直到 5 年前
        1
  •  1
  •   hd1 Madan Sapkota    5 年前

    我查看了您的代码,并提出以下建议:

    • ssc c e 它是可运行的。具体来说,你不会告诉我们游戏是什么样子的。

    • 日志模块是您的朋友,而不是将打印语句放在您的代码上。

    • y_loc = y_loc 这是禁止的,把它处理掉。

    • command = input("You are " + map[curr_loc]["desc"] + ' what next? ') 不需要在每一个子句中,而是在if/elif语句链之后。

        2
  •  0
  •   heiheihang    5 年前

    在线

    if cmd == 'move' or cmd == 'go' or 'hiddenpath':
    

    if cmd == 'move' or cmd == 'go' or cmd == 'hiddenpath':