简体   繁体   English

如何多次运行程序而不必重复运行?

[英]How to run a program more than once without having to repeatedly run it?

My code is below.我的代码如下。 I am writing a Minecraft program that randomly places 5 blocks in a single program.我正在编写一个 Minecraft 程序,它在一个程序中随机放置 5 个块。 I am having trouble in the for statement, as I am trying to place 5 blocks randomly without placing one block and having to run the program 5 times.我在 for 语句中遇到问题,因为我试图随机放置 5 个块而不放置一个块并且必须运行程序 5 次。 I'm not sure what to put in the for statement either.我也不知道要在 for 语句中放什么。 Any help or suggestions are always appreciated.任何帮助或建议总是受到赞赏。 * The code below the def function statement are indented. * def 函数语句下面的代码是缩进的。

def placeRandomBlocks():
    global diamond_x, diamond_y, diamond_z
    pos = mc.player.getTilePos() # get player position
    for i in range(0, 5):

Firstly you define your method for placing a single block randomly.首先,您定义随机放置单个块的方法。

from random import randint

def placeRandomBlock():
    x, y, z = mc.player.getPos()
    mc.setBlock(randint(0,9) + x, randint(0,9) + y, z, 1)

and then you need to call that method, within a loop to place multiple blocks.然后您需要在循环中调用该方法以放置多个块。

for block in range(0, 5):
    placeRandomBlock()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM