2016-05-12 4 views
-1

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") 
+0

Haben Sie sogar versucht, es zu debuggen? – drum

+0

ja ich tat, es gab nichts zurück –

+0

TessellatingHeckler, habe irgendwelche Vorschläge, was ich stattdessen tun könnte ?? –

Antwort

0

Alle Ihre if-Anweisungen für den Kompass auslösen nicht, weil:

Kompass [0: 6] ist nicht gleich "w" oder "n" oder jede einzelne char ist es ein einzelnes Zeichen im Vergleich zu 5 Zeichen

machen Sie alles nur eine Zeichenfolge oder machen sie alle eine Liste der gleichen Größe, aber Sie müssen die gleiche Länge Liste auf die gleiche Länge zu vergleichen oder einen Strin vergleichen g zu einem String-nicht a: Liste [1] == Liste [5] # Wenn Sie sehen, was ich meine

+0

also ersetzen Sie, wenn Kompass mit der Liste Teil –

+0

1) Ändern Sie die "Kompass [0: 6]" if-Anweisungen zu "Kompass [0]" und wie Sie wissen, sicher sein, die Zahl nach, da es keine Benutzereingabe ist geprüft – HBez

+0

versuchte das und es hat nicht funktioniert ??? –

0

1) Ändern Sie den Einführungstext, die Sie, wie Sie es verwenden, so ist es richtig:

print ("You can also print, n, s, e, or w, to move in those directions") 

zu

print ("You can also print, n 4, s 4, e 4, or w 4, to move 4 in those directions") 

weil ich gestolpert, als ich wasn‘ Ich erwarte, dass es Entfernungen braucht, und das tut es auch.

2) Die Zeilen if compass[0:5] == "n" versuchen, eine 5-stellige Zeichenkette mit einer 1-Zeichenkette zu vergleichen und festzustellen, ob sie gleich sind. Sie werden niemals gleich sein.

Ändern Sie sie in if compass[0] == "n" für die vier Richtungen, dann Befehle wie "n 4" wird 4 Einheiten nach Norden bewegen.

Ohne Bezug auf die Frage, aber zum Spaß habe ich begonnen, es neu zu schreiben, um Code-Duplizierung zu reduzieren und die Variablen umzubenennen. Es wurde schließlich objektorientiert; Es ist jetzt aufgeteilt zwischen einer Front-End-Kommunikationsschnittstelle, die Befehle und verarbeitet sie ein wenig für die Bequemlichkeit, und einem Remote-Rover - hypothetisch könnte es jetzt mehrere Rover unterstützen, wenn die Befehlsschnittstelle aktualisiert wurde.

Der Rover ist auf dem Mars, also kann er nicht direkt auf den Bildschirm drucken, er kann nur Text zurückgeben. Der Rover ist die Autorität, welche Befehle er ausführen kann und was er nicht akzeptieren kann. Daher sendet die Schnittstelle Befehle an sie. Wenn eine Methode für den gesendeten Befehl benannt ist, wird sie ausgeführt, andernfalls wird ein Fehler ausgegeben.

Die Befehls- und Parameterbehandlung ist auf einzelne Befehle oder Befehle mit einer oder zwei Zahlen als Parameter reduziert und sollte daher robuster in Bezug auf Fehler sein.

Run it on repl.it here

import random 

objects = [ # Things it can dig up 
    'David Bowie Life On Mars Record', 'A DeLorean', 
    'A Hover Board', 'Star Man', 'Water' 
    ] 

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 ("************************************") 

class Rover: 
    def __init__(self): 
     self.x, self.y = 0, 0 
     self.resting, self.available = True, True 

    def ready(self): 
     self.resting = False 
     return "Rover is awake and ready for use" 

    def dig(self): 
     return "The rover found {}!".format(random.choice(objects)) 

    def moveto(self, x, y): 
     self.x, self.y = x, y 
     return "The Mars Rover is at position {}, {}".format(self.x, self.y) 

    def rest(self): 
     self.resting, self.available = True, False 
     return "Rover is done for the day" 

    def instruct(self, command, params): 
     if self.resting and command != "ready": 
      return "Error, cannot complete command\nPlease 'ready' the rover to continue" 
     else: 
      try: 
       return getattr(self, command)(*params) 
      except: 
       return "Rover command error" 

rover = Rover() 
print("The Mars Rover is at position {}, {}".format(rover.x, rover.y)) 

while rover.available: 
    command = input("Please enter your command for the rover:").split(' ') 
    command, params = command[0], command[1:] 

    if all(p.isdigit() for p in params): 
     params = list(map(int, params)) 
    else: 
     print("Command error: parameters X Y need to be numbers") 
     continue 

    if command == "return": 
     command, params = "moveto", (0, 0) 

    elif command in ("n", "e", "s", "w"): 
     if not params: params = [1] 
     distance = params[0] 
     if command == "n": params = (rover.x, rover.y+distance) 
     if command == "e": params = (rover.x+distance, rover.y) 
     if command == "s": params = (rover.x, rover.y-distance) 
     if command == "w": params = (rover.x-distance, rover.y) 
     command = "moveto" 

    print(rover.instruct(command, params)) 
+0

nicht wirklich immer noch etwas zurück bekommen? –

+0

Der Code wird geschrieben, um eine Nummer mit den Himmelsrichtungen zu benötigen. Du weißt nicht, dass es eine Nummer braucht, also ... du hast es nicht geschrieben? – TessellatingHeckler

Verwandte Themen