嘿,我正在尝试这个额外的信用档案,我已经附在这里了。
https://drive.google.com/file/d/0B_K2RFTege5uRF9IeGJiYllQbGs/view?usp=sharing
我正在使用Python 3.4.3
请从上面的列表中输入方向:上
请输入您想要采取的步数:5
Traceback (most recent call last):
File "D:\Downloads\programming-assignment-5.py", line 63, in <module>
main()
File "D:\Downloads\programming-assignment-5.py", line 61, in main
move_robot(direction,steps)
File "D:\Downloads\programming-assignment-5.py", line 28, in move_robot
start[1].append(int(step))
AttributeError: 'int' object has no attribute 'append'
这是我到目前为止的代码:
import math
import sys
def calculate_distance(pos1, pos2):
pos1 = [x,y]
pos2 = [x,y]
#distance = math.sqrt((((-)**2)+((-)**2))
def command_list():
print("Utilize the folowing commands")
print("Up, Down, Left, Right")
print("press 'E' to Exit")
def move_robot(step_sequence,step):
x = 0
y = 0
start = [0,0]
#end = [x,y]
#print(start)
moveRobot = [["up"],["down"],["left"],["right"]]
while step_sequence != "e":
if moveRobot[0][0] == step_sequence:
#x = x + int(step)
#start.append(int(step))
start[1].pop(0)
start.insert[1](step)
print("you moved UP",step, "space")
print(start)
elif moveRobot[1][0] == step_sequence:
print("You moved DOWN 1 space")
down = 0
elif moveRobot[2][0] == step_sequence:
print("You moved LEFT 1 space")
left = 0
elif moveRobot[3][0] == step_sequence:
print("You moved RIGHT 1 space")
right = 0
elif step_sequence == "e":
sys.exit()
elif step_sequence != (moveRobot[0][0] or moveRobot[1][0] or moveRobot[2][0] or moveRobot[3][0]):
print()
print("You have Entered an Invalid command.")
print("Please enter one of the following commands.")
print("Up, Down, Left, Right")
print()
step_sequence = str(input("Please enter a direction from the list above.: "))
steps = int(input("Please enter how many steps you would like to take.: "))
print(start)
return
def main():
command_list()
print()
direction = str(input("Please enter a direction from the list above.: "))
steps = int(input("Please enter how many steps you would like to take.: "))
move_robot(direction,steps)
#calculate_distance(move_robot(direction,steps))
main()
答案 0 :(得分:1)
if matrix[0][1] == command :
这将为您提供索引超出范围的错误,因为matrix
是4x1二维列表,[0][1]
只有4x2时才是有效坐标。
快速解决方法是切换每个坐标对的顺序,例如: [0][1]
变为[1][0]
。但你应该只使用一维列表,例如matrix = ["up", "down", "left", "right"]
。然后,您的条件会简化为if matrix[0] == command:
,if matrix[1] == command:
等。或者,您根本不应该使用任何类型的列表,只需直接插入字符串文字,例如if command == "up":
答案 1 :(得分:1)
使用更新的代码,您试图附加到int,您不能这样做,而是想要设置start[1] = int(step)
所以开头看起来像是:
while step_sequence != "e":
if moveRobot[0][0] == step_sequence:
#x = x + int(step)
start[1] = int(step)
print("you moved UP",step, "space")
print(start)
答案 2 :(得分:0)
matrix
是一个包含多个单元素数组的数组,因此您的索引是相反的:
if matrix[0][0] == command :
print ("you moved up 1 space")
if matrix[0][1] == command :
print ("you moved up 1 space")
if matrix[0][2] == command :
print ("you moved up 1 space")
if matrix[0][3] == command :
print ("you moved up 1 space")
if command == "c":
sys.exit()