Python 如何让多个条件同时执行?
我决定尝试做一个海龟项目来学习随机命令。我的问题在这段代码中:Python 如何让多个条件同时执行?,python,turtle-graphics,Python,Turtle Graphics,我决定尝试做一个海龟项目来学习随机命令。我的问题在这段代码中: pos = 360 while a.ycor()<pos: a.forward(randint(1,5)) while b.ycor()<pos: b.forward(randint(1,5)) while c.ycor()<pos: c.forward(randint(1,5)) while d.ycor()<pos: d.forward(randint(1,5)) while e.ycor()&l
pos = 360
while a.ycor()<pos:
a.forward(randint(1,5))
while b.ycor()<pos:
b.forward(randint(1,5))
while c.ycor()<pos:
c.forward(randint(1,5))
while d.ycor()<pos:
d.forward(randint(1,5))
while e.ycor()<pos:
e.forward(randint(1,5))
pos=360
当a.ycor()时,您可以尝试使用逻辑运算符和:
pos = 360
while a.ycor()<pos and
b.ycor()<pos and
c.ycor()<pos and
d.ycor()<pos and
e.ycor()<pos:
a.forward(randint(1,5))
b.forward(randint(1,5))
c.forward(randint(1,5))
d.forward(randint(1,5))
e.forward(randint(1,5))
pos=360
虽然a.ycor()我不能100%确定您要做什么,因此假设您希望同时运行多个函数,请使用或模块。
快速多处理示例:
import multiprocessing, time
def aFunction(aVar):
time.sleep(5)
print(f"Ye {aVar}")
if __name__ == '__main__':
multiprocessing.freeze_support()
pool = multiprocessing.Pool(4)
pool.map(aFunction, [X for X in range(4)])
input("Press Return to exit")
为了避免重复,可以将变量和方法放在字典中,然后循环使用它们
pos = 360
d = {a: a.ycor, b: b.ycor, c: c.ycor, d: d.ycor, e: e.ycor}
for k, v in d.items():
while v() < pos:
k.forward(randint(1, 5))
pos=360
d={a:a.ycor,b:b.ycor,c:c.ycor,d:d.ycor,e:e.ycor}
对于d.项()中的k,v:
而v()
就像是而条件1、条件2和条件3等等?你能解释一下你想用上面的代码输出什么吗?仅计算前进步数是不够的?您可能希望看到:您是否获得了所需的输出?请阅读。