Advertisement
Guest User

Assignment 2 Electric Boogaloo

a guest
Feb 27th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. import turtle #Before we do anything, we must import the turtle module.
  2. import time #imports the time module to add a delay after each text section is printed.
  3. #We need to initialize some flag variables to keep track of possible errors
  4. #error_section_type = False
  5.  
  6. errors_present = False
  7.  
  8.  
  9. header = input()
  10. print('***Header:', header) #Prints the header into the console for clarity.
  11. time.sleep(0.5)
  12. sections = int(input()) #Creates an int variable equal to the number of text/image sections.
  13.  
  14.  
  15. line = 1 #creates a variabe to keep track of the line the code is currently on.
  16. i = 1 #initializes a simple counter variable to be used in the while loop below. increments for every section passed:
  17. checksum = 0 #creates a variable to keep track of checksum errors.
  18.  
  19.  
  20. while i <= sections: #Creates a while loop. This will repeat until the counter variable is greater than the total number of allowed sections. All of the code will be in here basically.
  21. checksum = 0 #Resets the checksum variable for the next section counter.
  22. section_type = input() #Creates a variable to check the current section type. Only 'text' and 'image' will run code, any others will be skipped as seen below:
  23. if section_type == 'text': #If the section_type variable is 'text', the code will run differently as described below
  24. while True: #Text will repeat until an 'END' statement breaks the code
  25. text_input = input() #Creates a variable 'text_input' to keep track of the current input
  26. if text_input == 'END': #If the input is 'END', this signifies the end of this text section. This code will increase the section counter i and break the while loop above
  27. i = i+1 #update the number of sections iterated through
  28. break
  29. checksum = checksum + len(text_input) #updates the checksum variable based on the length of the text supplied
  30. print(text_input) #prints the text_input
  31. text_length = int(input()) #One the loop is broken by the END statement, we need to check for any check_sum errors. The text_length will be set to the value of the expected length as given by the input document
  32. if checksum != text_length: #If the two do not equal, an error is present somewhere in the code. This will define the expected length and executed length, as well as the section the error is present within.
  33. time.sleep(0.5) #adds some delay for aesthetics or something like that
  34. print('***Checksum error located for section', i-1)
  35. time.sleep(0.5)
  36. print('***expected length: ', text_length)
  37. errors_present = True #Errors have occurred. This can be updated (if not already)
  38. time.sleep(1) #delays the code for 1 second using the time module ########################################################################################################################################################################
  39. elif section_type == 'image': #if the section type is image, the below loop will run. ###STILL NEED RLE SUPPORT AND CHECKSUM!!!!!!!###
  40. t=turtle.Turtle() #initiates the turtle module in such a way it can be used using t. instead of turtle.
  41. t._tracer(500)
  42. t.reset() #resets the previous image, should one exist
  43. t.penup() #Just in case the pen happens to be down (which should be impossible but hey better safe than sorry
  44. image_input = input() #The first two values will be the size of the turtle window provided. We don't have to use loops here as we need to create variables.
  45. checksum = checksum + len(image_input) #Update the checksum value
  46. turtle_width = int(image_input) #The first image input is the width of the turtle window
  47. image_input = input()
  48. checksum = checksum + len(image_input)
  49. turtle_height = int(image_input) #The second is the height
  50. t.screen.setup(height=turtle_height,width=turtle_width) #Set the width and height as such
  51. t.goto(-turtle_width/2,turtle_height/2) #moves the turtle to the top left corner of the window
  52. for iteration_height in range(turtle_height): #We'll have a for loop nested in another for loop to build the image row by row
  53. x_corr = t.xcor() #saves these values to be manipulate later. This can be used to move the turtle down after the end of a row is complete
  54. y_corr = t.ycor()
  55. for iteration_width in range(turtle_width): #Number of repetitions is equal to the width of the window.
  56. x = t.xcor() #Saves the turtle's corruent position so it can be updated in the statements below
  57. y = t.ycor()
  58. image_input = input()#saves the input. We are saving this value in order to check the length later
  59. turtle_action = int(image_input) #creates an int version of the variable above to update the turtle
  60. ##if the text == 'end' we must do SOMETHING!
  61. elif turtle_action == 0: #if the input is equal to 0
  62. t.goto(x+1,y) #nothing is needed to be drawn, so we can safely move to the next variable
  63. elif turtle_action == 1: #if the input is equal to 1.
  64. t.pendown() #put the pen down to draw...
  65. t.goto(x+1,y) #move exactly one pixel across...
  66. t.penup() #and put the pen back up.
  67. else: #If the data is not 0, 1, 'END', or a negative RLE, it is an unknown data type and an error message should be printed
  68. print('***Error - Bad numerical data', turtle_action)
  69. print('***Value ignored, continuing data:')
  70. t.goto(x_corr,y_corr-1)
  71.  
  72. else: #if the section type is unrecognized
  73. print('***Unknown section type: "', section_type, ' " - Section', i) #prints error message
  74. time.sleep(0.5)
  75. print('***Skipping bad data:')
  76. errors_present = True #Errors have occurred. This can be updated (if not already)
  77. while True: #Loops until the input type is 'END' to ignore unusable lines of data
  78. bad_section_type = input() #Loops through every line
  79. if bad_section_type == 'END': #when the end statement is reached
  80. i = i+1 #update the number of sections iterated through
  81. break
  82. checksum = checksum + len(bad_section_type)
  83. bad_length = int(input()) #One the loop is broken by the END statement, we need to check for any check_sum errors. The bad_length will be set to the value of the expected length as given by the input document
  84. if checksum != bad_length: #If the two do not equal, an error is present somewhere in the code. This will define the expected length and executed length, as well as the section the error is present within.
  85. time.sleep(0.5) #adds some delay for aesthetics or something like that
  86. print('***Checksum error located for Section', i-1)
  87. time.sleep(0.5)
  88. print('***Expected length:', bad_length)
  89. errors_present = True #Errors have occurred. This can be updated (if not already)
  90.  
  91.  
  92. print('***End of input')
  93. if errors_present == True:
  94. print('***Errors were found in the program.')
  95. #elif section_type == 'image':
  96. # #image shit mmhmm yeah baby
  97. #t=turtle.Turtle() #This allows us to type t. instead of turtle. for every turtle module command.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement