我编写了一个机器人,它运行的模块包含执行特定过程所需的命令。截至目前,用户输入了所需的命令,我的代码使用if语句来确定他们输入的命令,并运行相应的命令。但是,必须先完成命令,然后用户才能输入另一个命令。
现在我想执行以下操作:让用户输入命令,启动命令,并在命令运行时重新运行命令以获取用户输入。 例如,用户输入向前移动,机器人开始移动,然后用户改变命令以在机器人向前移动的中途向后移动,并且机器人重置并开始向后移动以作为响应。
下面是运行模块并询问用户输入的while循环的代码。如果您对如何实现此目标或是否需要任何澄清有任何想法,请与我们联系。我是一名高中生,仍然在学习如何编码,所以任何帮助都会非常感激。
提前致谢。
最好,
克里斯托弗
#runs all the modules and gets user input
while True:
defultPosition()
command = raw_input("Enter move forward, move backward, turn or cancel: ")
defultPosition()
if command == "cancel":
break
if command == ("move forward") or (command == "move backward"):
speedInput = input("Enter the desired speed: ")
distanceInput = input("Enter the number of inches you wish the robot to move (must be a factor of 5): ")
if command == "turn":
speedInput = input("Enter the desired speed: ")
degrees = input("Enter the number of degrees for the robot to move: ")
print ("\nINPUTED COMMAND: %s \n" % command)
if command == "move forward":
#run the moveForward module
print "Initiating command\n"
moveForward(speedInput, distanceInput)
print "Finished command; restarting and waiting for another input \n"
if command == "move backward":
#run the moveBackward module
print "Initiating command\n"
moveBackward(speedInput, distanceInput)
print "Finished command; restarting and waiting for another input \n"
if command == "turn":
#runs the turn module
print "Initiating command\n"
turnCounterClockwise(speedInput, degrees)
print "Finished command; restarting and waiting for another input \n"
答案 0 :(得分:0)
我已经确定使用线程是我问题的最佳解决方案。它能够在程序中的任何一点发送终止信号,它将使用新参数重新启动线程。
如果有人想查看我使用的代码,请告诉我。