2016-05-12 4 views
-1

火星探査機は以下のプログラムで操作します。最初にローバーを始動する準備ができていることを入力する必要があります。次に、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") 
+0

あなたはそれをデバッグしようとしましたか? – drum

+0

はい、私は何も返しませんでした –

+0

TessellatingHecklerは、私が代わりに何をすることができるかに関する提案を受けましたか? –

答えて

0

あなたのコンパスのためのステートメントがトリガされていない場合ためのすべて

Run it on repl.it here

コンパス[0:6]は等しくない場合に、 "W" または "N" または任意の単一charそれは5文字と比較される単一の文字です

すべてを文字列にするか同じサイズのすべてのリストにしますが、同じ長さのリストを同じ長さと比較するかstrinを比較する必要がありますg:文字列ではなくa:list [1] == list [5]#あなたが私が何を意味するかを見たら

+0

コンパスでリスト部分を置換する場合 –

+0

1) "compass [0:6]" if文を "compass [0]"に変更し、ユーザ入力ではないので、プルーフされたまだ – HBez

+0

それを試して、それは動作しませんでしたか? –

0

1)あなたはどのようにそれを使用するように指示紹介テキストを変更しますので、それは正しいです:

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

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

に、私は「wasnたときに私がつまずいているためそれが距離を必要とすることを期待し、それはありません。

2)行if compass[0:5] == "n"は、5文字の文字列と1文字の文字列を比較し、それらが同じかどうかを確認しようとしています。彼らは決して同じではありません。

4方向についてはif compass[0] == "n"に変更すると、「n 4」などのコマンドは4単位北へ移動します。


質問には関係ありませんが、楽しいことに、私はコードの重複を減らして変数名を変更するために書き直し始めました。それはオブジェクト指向になった。コマンドを受け取り、便宜上少しだけ処理するフロントエンド通信インターフェースと、リモート・ローバーに分割されました。コマンド・インターフェースがアップグレードされた場合、複数のローバーをサポートすることができます。

ローバーは火星にありますので、スクリーンに直接印刷することはできません。テキストを返すことができます。ローバーは、どんなコマンドを受け入れることができるのか、受け入れることができないのかを判断する権限を持ちます。そのため、インターフェースはコマンドを送信します。送信するコマンドに名前が付けられているメソッドがあれば、それを実行します。

コマンドとパラメータの処理は、単一のコマンド、またはパラメータとして1つまたは2つの数値を持つコマンドに縮小されており、エラーのためにエラーに関してより堅牢でなければなりません。

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

まだ何も戻っていませんか? –

+0

コンパスの指示に沿って数字が必要なコードが書かれています。あなたはそれが数字を必要としていることを知らないので、あなたはそれを書いていませんでしたか? – TessellatingHeckler

関連する問題