In dem Programm unten sollst du einen Marsrover betreiben. Sie müssen zuerst bereit eingeben, um den Rover zu starten. Geben Sie dann moveto und eine Reihe von Koordinaten ein, und geben Sie schließlich n, s, e, w ein, um den Rover an der gewünschten Stelle zu bewegen und einen Ausdruck wie unten gezeigt zurückzuschicken. Aber wenn man einen dieser vier Buchstaben eingibt, tut das Programm nichts, es fragt einfach nach einer anderen Eingabe, nach irgendwelchen Ideen?Programm fährt fort, nach Eingabe zu fragen, obwohl Eingang gegeben wird (Mars Rover 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")
Haben Sie sogar versucht, es zu debuggen? – drum
ja ich tat, es gab nichts zurück –
TessellatingHeckler, habe irgendwelche Vorschläge, was ich stattdessen tun könnte ?? –