火星探査機は以下のプログラムで操作します。最初にローバーを始動する準備ができていることを入力する必要があります。次に、movetoと座標のセットを入力し、最後にn、s、e、wとタイプして指定された場所に移動し、下に示すようにプリントを返します。しかし、これらの4文字のうちの1文字を入力すると、プログラムは単に他の入力、任意のアイデアを求めるだけで何もしません。プログラムは入力にもかかわらず入力を要求し続ける(火星探査機Python)
print ("************************************")
print ("Welcome to the Mars Rover Control HQ")
print ("Print \"ready\" as the first command when prompted")
print ("Print dig or moveto (with coordinates) afterwards")
print ("You can also print, n, s, e, or w, to move in those directions")
print ("************************************")
import random # allows computer to grab random commands, for the objects to dig
x = 0 # this is the starting point of the rover
y = 0
print("The Mars Rover is currently at position", x, y)
TARDIS = "no" # at orgin rover it at rest and cannot move(this variable will change if command is ready)otherwise loop does not work!
Mars = 1 # this acts like a while True, as long as Mars is 1 it will create an infinite loop
while Mars == 1: # starts the program
commandT = input("Please enter your command for the rover:") # this asks for the a string for commandT
space0 = int(commandT.find(" ")) # find the blanks spaces between direction and units
lengthC = len(commandT) # this will find the length of the command
compass = commandT[0:space0] # find first character to the space between direction and number
commandT2 = commandT[7:lengthC] # moveto takes up 7 characters and will count up to the total length of string
space1 = int(commandT2.find(" ")) # this will find the blank space in the moveto command
if commandT == "ready": # this will ready rover
print ("Rover is awake and ready for use")
TARDIS = "ready"
# else:
# print"Please ready the rover!" # must ready rover to continue into other loop, error message will show
if TARDIS == ("ready"):
if commandT == "dig": # if command was dig, computer will randomly choose between the 5 choices
objects = random.choice(['David Bowie Life On Mars Record', 'A Dalorean', 'A Hover Board', 'Star Man', 'Water'])
print ("The rover found %s!" % objects)
if commandT[0:6] == "moveto": # if command is in moveto format
x = (commandT2[0:space1]) # will convert orginal coordinates to values
y = (commandT2[space1:lengthC])
print("The Mars Rover has moved to position", x, y)
# this is for the compass directions if chosen......
if compass[0:6] == "n": # if chosen to move north
value = int(commandT[space0:lengthC])
y += value # moves up positive y axis
print("The Mars Rover has moved to position", x, y)
if compass[0:6] == "s": # if chosen to move south
value = int(commandT[space0:lengthC])
y -= value # moves down negative y axis
print("The Mars Rover has moved to position", x, y)
if compass[0:6] == "e": # if chosen to move east
value = int(commandT[space0:lengthC])
x += value # moves up positive x axis
print("The Mars Rover has moved to position", x, y)
if compass[0:6] == "w": # if chosen to move west
movement = int(commandT[space0:lengthC])
x -= movement # moves down negative x axis
print("The Mars Rover has moved to position", x, y)
if commandT == "return": # if command is return
x = 0 # convert coordinates back to origin
y = 0
print("The Mars Rover has moved to position", x, y)
if commandT == "rest": # if command is rest
Mars = 2 # stops the loop because Mars=1 is no longer true!
print ("Rover is done for the day.")
else:
print("Error, cannot complete command") # these error messages will show if rover is not ready
print("Please ready the rover to continue")
あなたはそれをデバッグしようとしましたか? – drum
はい、私は何も返しませんでした –
TessellatingHecklerは、私が代わりに何をすることができるかに関する提案を受けましたか? –