循环时自动赋值变量

发布于 2025-01-18 07:18:22 字数 482 浏览 0 评论 0原文

p_dil_1、p_dil_2、p_dil_3、p_dil_4 ..... 是我已经制作的变量。

tg1 = p_dil_1
tg2 = p_dil_2
n_dil = 0

while True : 
    n_dil += 1
    # some movement here
    
    if n_dil == 10 :
        break
    if n_dil <= 10 : 
        tg1 = p_dil_2
        tg2 = p_dil_3
        continue 

最初的目的是

对于第一个循环,tg1=p_dil_1, tg2=p_dil_2

对于第二个循环,tg1=p_dil_2, tg2=p_dil_3

...

对于第十个循环,tg1=p_dil_10, tg2=p_dil_11

并结束循环

我怎样才能让循环变得简单?

p_dil_1, p_dil_2, p_dil_3, p_dil_4 ..... are variables I have already made.

tg1 = p_dil_1
tg2 = p_dil_2
n_dil = 0

while True : 
    n_dil += 1
    # some movement here
    
    if n_dil == 10 :
        break
    if n_dil <= 10 : 
        tg1 = p_dil_2
        tg2 = p_dil_3
        continue 

The original purpose is

For the first loop, tg1=p_dil_1, tg2=p_dil_2

For the second loop, tg1=p_dil_2, tg2=p_dil_3

...

For the tenth loop, tg1=p_dil_10, tg2=p_dil_11

and end the loop

How can I make the loop simple?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抹茶夏天i‖ 2025-01-25 07:18:22

这是一种让事情变得更简单、更干净的方法。

var listOfValues = [p_dil_1, p_dil_2, ..., p_dil_11] // etc.
for n_dil = 0; n_dil <= 10; n_dil += 1 {
    // Some Movement
    tg1 = listOfValues[n_dil]; tg2= listOfValues[n_dil + 1];
}

希望这有帮助。

Here is a way to make things a bit simpler and cleaner.

var listOfValues = [p_dil_1, p_dil_2, ..., p_dil_11] // etc.
for n_dil = 0; n_dil <= 10; n_dil += 1 {
    // Some Movement
    tg1 = listOfValues[n_dil]; tg2= listOfValues[n_dil + 1];
}

Hope this helps.

青芜 2025-01-25 07:18:22

您不能动态分配变量。使用列表并使用变量来引用列表中的位置。

list = [a, b, c, d, e, f]
index = 0
while index < 5:
  var1 = list[index]
  var2 = list[index + 1]
  # use var, var2
  index = index + 1

You can't assign variables dynamically. Use a list and use a variable to reference the position in the list.

list = [a, b, c, d, e, f]
index = 0
while index < 5:
  var1 = list[index]
  var2 = list[index + 1]
  # use var, var2
  index = index + 1
倥絔 2025-01-25 07:18:22

最好的方法是使用字典。另请检查您的逻辑,因为当 n_dil = 10 时,最后一个 if 语句将永远不会执行,因为循环首先中断。

p_dil_dict = {1: "foo", ..., 2: "bar"}
tg1 = p_dil_dict["1"]
tg2 = p_dil_dict["2"]

for n_dil in range(1, 10): # n_dil = 1, 2..., 8, 9
    # do something

    tg1 = p_dil_dict[n_dil]
    tg2 = p_dil_dict[n_dil]

请注意,如有必要,for 循环如何从 1 开始(尽管我建议从 0 开始)。此外,for 循环意味着您不需要中断 while 循环或设置和递增 n_dil

Your best approach would be to use dictionaries. Please also check your logic as when n_dil = 10 the last, if statement will never be executed as the loop, breaks first.

p_dil_dict = {1: "foo", ..., 2: "bar"}
tg1 = p_dil_dict["1"]
tg2 = p_dil_dict["2"]

for n_dil in range(1, 10): # n_dil = 1, 2..., 8, 9
    # do something

    tg1 = p_dil_dict[n_dil]
    tg2 = p_dil_dict[n_dil]

Notice how a for loop can be started at 1 if necessary (although I'd recommend starting at 0). Also the for loop means you don't need to break a while loop or set and increment n_dil.

冷月断魂刀 2025-01-25 07:18:22

您可以将P_DIL_1,P_DIL_2 ...等存储在列表中,您可以说 - p_dil []。您可以使用p_dil [0]p_dil [1]初始化TG1 TG2。然后使用n_dil = 2开始循环,并注意变量tg1从上一个迭代中获取tg2的值。

p_dil = [p_dil_1, p_dil_2, ....]
tg1 = p_dil[0]
tg2 = p_dil[1]
n_dil = 2
while n_dil <= 10:
    tg1 = tg2
    tg2 = p_dil[n_dil]
    n_dil += 1

You can store your p_dil_1, p_dil_2... etc in a list, lets say - p_dil[]. You can initialize tg1 and tg2 with p_dil[0] and p_dil[1]. Then start your loop with n_dil = 2 and note that the variable tg1 takes the value of tg2 from the previous iteration.

p_dil = [p_dil_1, p_dil_2, ....]
tg1 = p_dil[0]
tg2 = p_dil[1]
n_dil = 2
while n_dil <= 10:
    tg1 = tg2
    tg2 = p_dil[n_dil]
    n_dil += 1
怪我入戏太深 2025-01-25 07:18:22

使用for循环怎么样?

tgs = [p_dil_3, p_dil_4, ..., p_dil_11]
tg1, tg2 = p_dil_1, p_dil_2

for new_dil in range(tgs):
    # some movement....
    tg1 = tg2
    tg2 = new_dil

What about using for loop?

tgs = [p_dil_3, p_dil_4, ..., p_dil_11]
tg1, tg2 = p_dil_1, p_dil_2

for new_dil in range(tgs):
    # some movement....
    tg1 = tg2
    tg2 = new_dil
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文