Hello I am doing a python turtle assignment and I wanted to
Hello, I am doing a python turtle assignment, and I wanted to know if I\'m on the right track.
Assignment Overview
In this assignment you will write a program to have a turtle follow commands given in a text file called moves.txt. The file will contain one command per line. There will be one of these three commands on each line: goto, jumpto, or color. They will be formatted as follows:
goto # x-coordinate #y-coordinate
jumpto #x-coordinate #y-coordinate
color
For goto and jumpto, the command and coordinates will be separated by #; color is a command itself. It is a legitimate instruction if there are spaces between fields on a line. For example,
goto # 45 # 100
is a valid input
Here is how the commands work:
If the command is goto #x #y, then the turtle will go to the point (x,y) drawing a line as it moves.
If the command is jumpto #x #y, then the turtle will go to the point (x,y) without drawing a line. (remember penup() )
If the command is color, turtle will generate a random color and set its color to the new color so next drawing will be in that color.
Note: The commands goto, jumpto, and color can be upper or lowercase.
Input validation
In this program you will also validate the input from the file. That is, you will check whether the “input” that is the instruction from the file is valid or not. If it is an invalid instruction, then the program will append the line number to a list, skip the line and proceed with the next line.
When the program ends, print the list of invalid line numbers on the shell screen.
Some examples of invalid inputs:
go to #30#40 (go to is not a valid command)
color #30 #40 (color cannot be followed by non-white-space character)
jumpto #a2#34 (a2 is not a valid integer)
goto 30 #40 (need a separator before 30)
goto # # 40 (coordinate missing)
jumpto #30 (coordinate missing)
goto #30 #40 67 (not a valid coordinate)
goto #30#40#60 (too many coordinates)
Think about other possible mistakes or invalid inputs in the file and append line numbers in the list.
Start with implementing the goto#x#y command. First make sure that it works with valid input. For example if oves.txt has the following, then turtle should draw a square on the screen.
goto #0 #100
goto # 100 #100
goto #100 #0
goto #0 # 0
This is the code that I have thus far:
#!/usr/bin/python
fo = open(\"moves.txt\", \"r\")
lst = []
lin_num = 0
lines = fo.readlines()
for line in lines:
lin_num = lin_num+1
line = line.split(\'\ \')
characters = line[0].split(\"#\")
chars = []
for character in characters:
chars.append(character.split(\" \")[0])
if chars:
if str(chars[0]) == \'goto\' or str(chars[0]) == \'jumpto\':
print chars[0]
if len(chars) == 3:
try:
int(str(chars[1]))
int(str(chars[2]))
except ValueError:
lst.append(lin_num)
else:
lst.append(lin_num)
elif chars[0] == \'color\':
if len(chars) != 1:
lst.append(lin_num)
else:
lst.append(lin_num)
else:
lst.append(lin_num)
print \"Invalid Lines are:\"
for line in lst:
print line
Solution
Your algo looks fine...
Other algo should be like this
Read each file till the end of file
Get first line
Remove all blank space from line
Get string till\'#\' character or up to last character
if string is color than it should be last character in line
else if string is \'goto\' or \'jumpto\' then parse further line
Get string till 2nd \'#\'
if extracted string is number then
extract remaining string
if remaining string is again a number then this is a correct line
otherwise line is wrong


