我编写了一个机器人,它运行包含执行特定过程所需命令的模块。现在,用户输入所需的命令,我的代码使用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"
我已经确定使用线程是解决我的问题的最佳方法。它能够在程序中的任何一点发送终止信号,并用新参数重新启动线程
如果有人想查看我使用的代码,请告诉我
相关问题 更多 >
编程相关推荐