【问题标题】:python changing location using for looppython使用for循环更改位置
【发布时间】:2022-12-31 22:05:50
【问题描述】:

我想通过放置命令来更改位置,但在 for 循环中遇到了麻烦

我想要的只是放 R R R U D D 并获得 (3,4) 位置

这是我的代码

x,y = 第一个起点 N=地图的大小

N=5
x,y = 1,1

我定义左(-1,0),右(1,0),上(0,-1),下(0,1)

def L(x,y):
    if x>1 and x<N and y>1 and y<N:
        x=x
        y=y-1
        return(x,y)
    else:
        return(x,y)
def R(x,y):
    if x<N and y<N:
        x=x
        y=y+1
        return(x,y)
    else:
        return(x,y)
def U(x,y):
    if x>1 and x<N and y>1 and y<N:
        x=x-1
        y=y
        return(x,y)
    else:
        return(x,y)
def D(x,y):
    if x<N and y<N:
        x=x+1
        y=y
        return(x,y)
    else:
        return(x,y)

输入命令

move_type=L(x,y),R(x,y),U(x,y),D(x,y)


num = [*map(int, input().split())]

放数 [1 1]

更改位置 - 这是我遇到麻烦的地方

for i in num:
    x,y = move_type[i]
    print(x,y)

**结果是这样的

1 2
1 2

我期望 (1,2) (1,3)

我的代码有什么问题 请帮帮我**

【问题讨论】:

    标签: python loops for-loop location


    【解决方案1】:

    像那样运行方法

    num = [*map(int, input().split())]
    x, y = num
    
    # move_type=L(x,y),R(x,y),U(x,y),D(x,y) # if you use this way then x and y values are always same. 
    # for example if x and y are 1 then L, R, U, D are always have same x and y value 1.
     
    
    move_type = L, R, U, D
    
    for i in num:
        x, y = move_type[i](x, y)
        print(x, y)
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2013-08-14
      • 2019-07-25
      • 2014-04-05
      • 2021-11-23
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多