我编写了一个机器人,它运行包含执行特定过程所需命令的模块。现在,用户输入所需的命令,我的代码使用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"