Home Schedule Guides Study Material

Lab 3: Practice with Python and Thonny (10 pts)

Answer any questions (bold) as comments in your program like this:

            # This is a comment to answer a question.
        

When you complete the assignment upload lab03.py to Moodle!

Part I: Creating and Saving Programs in Thonny

  1. Locate your "CS102" folder that you created in lab 1
  2. Create a folder inside "CS102" called "lab03"
  3. Open the Thonny editor
  4. Thonny usually opens with the last file(s) you had open
  5. Click the "X" on the tabs in the program area to close any open editors
  6. Create a new file using either:
    • The icon that looks like a piece of blank paper near the top left of the window
    • The menu options File -> New
  7. You should see a new tab in your editor labeled "<untitled>"
  8. Save the file using either:
    • The icon that looks like a blue floppy disk near the top left of the window
    • The menu options File -> Save
  9. When propted for a save location, navigate to your "lab03" folder you created previously
  10. Name your file "lab03.py"
  11. You should now see the tab label has changed to "lab03.py"
  12. Click inside the "lab03.py" window to start editing
  13. Submit lab03.py to Moodle when you are finished

Part II: Using print()

  1. Add the folllwing lines of code to your program and run it (use the green play button next to the little bug icon)
                        
                            sum = 2+ 3
                            print(sum)
                        
                    
  2. Change the print statement to:
                        
                            print('The sum is: ' + sum)
                                
                            
  3. Run the code you just typed
  4. What happened? What window(s) showed you an error message? On what line did the error occur? Why do you think you got the error?
  5. Fix the error by changing the line to be:
                        
                            print('The sum is: ' + str(sum))
                        
                    
  6. Run the code
  7. What do you think the str() function does?

Part III: Mathematical!

  1. Add a two blank lines to your code and then the following lines:
                    
                        
                            j = 5
                            k = 10
                            n = j * k
                            n = n + 1
                        
                    
                
  2. What do you think the value of n will be?
  3. Add a statement to print out the value of n and verify your guess.
  4. Add the lines:
                    
                        
                            money = 754.45
                            money += 10
                            print(money)
                        
                    
                
  5. What do you think the value of money will be? What do you think += does? What other operators can you use instead of + (-, *, /, //, %)? Why might this kind of statement be useful?
  6. Add the following lines and run the program:
                    
                        
                            x = 5 * 12 + 6 / 8 - 4 ** 2
                            print(x)
                        
                    
                
  7. What is the value of x?
  8. Change the expression so it is evaluated in this order from left to right (HINT: The same way you do it in math class):
    1. addition/subtraction
    2. multiplication/division
    3. exponents
  9. What is the new answer?

Part IV: Word Play

  1. Add the following lines and run the program
                    
                        
                            first_word = "computer"
                            second_word = "science"
                            print(first_word + second_word)
                        
                    
                
  2. What happened?
  3. Add the following line and run the program
                    
                        
                            print(first_word * 2)
                        
                    
                
  4. What happened?
  5. Write a statement that prints out "computer science" ( DO NOT CHANGE THE VARIABLES! )
  6. Write a statement that prints "computercomputer sciencescience" ( DO NOT CHANGE THE VARIABLES! )

Extra Practice:

If you finish early, here are some things you can work on (none of these are required for the assignment, but are good practice).

Fun With print()

  1. Add the lines:
                    
                        
                            my_text = 'foobar'
                            print('This works too:', my_text)
                        
                    
                
  2. Run the code
  3. What happened?
  4. Change the line to be:
                    
                        
                            print('This works too:', my_text, sep='!')
                        
                    
                
  5. Run the code
  6. What happened? What do you think sep does?
  7. Change the line and add another sum:
                    
                        
                            print('This works too:', my_text, my_text, sep='!')
                        
                    
                
  8. Were you right about sep ?
  9. Add the lines:
                    
                        
                            print('This can work also:', end=' ')
                            print(my_text)
                        
                    
                
  10. Run the code
  11. What happened? How is this different from how print normally works? What do you think end does?
  12. Try putting the sum variable after the
                        
                            print('This can work also:', end=' ', my_text)
                        
                    
  13. What happened? Why do you think this occurred?
  14. Change the line to:
                        
                            print('This can work also:', my_text, end=' ')
                        
                    
  15. Run the code
  16. Were you right about the error?
  17. Save your work (floppy icon or menu)

Create a Simple Image

  1. Download a copy of media.py and copy it into your "lab03" folder
  2. Add the following to the top of your lab03.py file:
                    
                        
                            import media
                        
                    
                
  3. At the bottom of lab03.py add the following:
                    
                        
                            picture = media.makeEmptyPicture(300,300, media.blue)
                            media.show(picture)
                            media.quit()
                        
                    
                
  4. Run the code
  5. What happens? What does changing the numbers do? Can you change the color?
  6. Add the following line between the assignment to picture and the media.show() function
                        
                            picture.addRect(media.red, 10, 10, 10, 10, True)
                        
                    
  7. What happens? Can you change the color? What does changing the numbers do? What does changing True to False do?