Hello I am doing an assignment involving turtle on python an
Hello, I am doing an assignment involving turtle on python, and wanted to know if I can get help getting started
An Overview of what I\'ve got to do:
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
Also:
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.
Solution
#!/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

