Difference between revisions of "CSC111 Lab 2 2015"

From dftwiki3
Jump to: navigation, search
(First modification)
Line 170: Line 170:
  
 
(don't worry if you get extra spaces in your output)
 
(don't worry if you get extra spaces in your output)
 
==Hard problem of the day==
 
 
* This problem is a bit harder, and requires some thinking...
 
 
* Write a program that
 
** asks the user for number between 0 and 20
 
** Assume the user enters 7.
 
** The program then prints 7 stars (*) on the line, followed by 13 exclamation marks (!)
 
** In general, the program prints a number of stars equal to the number entered by the user, and continues with exclamation marks such that the total number of stars plus exclamation marks is 20.  Always.
 
 
* Examples of how your program should work (user input underlined):
 
 
      How many stars? <u>'''10'''</u>
 
      * * * * * * * * * * ! ! ! ! ! ! ! ! ! !
 
 
      How many stars? <u>'''20'''</u>
 
      * * * * * * * * * * * * * * * * * * * *
 
 
      How many stars? <u>'''1'''</u>
 
      * ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
 
  
 
=The '''input()''' Function=
 
=The '''input()''' Function=

Revision as of 19:05, 3 February 2015

--D. Thiebaut (talk) 17:01, 3 February 2015 (EST)


This lab deals with loops in python. Feel free to work in pairs, share insights and help your neighbors during the lab. This is true of all labs. While some homework assignments will have to be done as individual work, the labs are a good time to cooperate with neighbors or work in pairs.


Definite for-loops


Loops that use the range() function


  • For this section of the lab, open IDLE and create the different program sections below in the Python shell window.


>>> for n in range( 4 ):
	print( n )

>>> for n in range( 1, 4 ):
	print( n )

>>> for n in range( -1, 4 ):
	print( n )

>>> for n in range( 5, 10 ):
	print( n )

>>> for n in range( 0, 10, 2 ):
	print( n )

>>> for n in range( 1, 10, 2 ):
	print( n )

>>> for n in range( 10, 0, -2 ):
	print( n )


  • Do you understand how the range() function works?
    • If you give it only 1 number, it will generate a series of numbers starting at 0 and going up to that number, but not including it, in steps of 1.
    • If you give it 2 numbers, it will use the first one as the start, and the second at the stop, and will generate all the integers between the two, in steps of 1. The stop number is never used.
    • IF you give it 3 numbers, the 3rd one is the step. It can be positive or negative.


Challenge #1: Range-Controlled Loops

QuestionMark1.jpg


Now, your turn:

List of even numbers
write a loop that prints all the even numbers between 4 and 20; 4, and 20 included.
List of odd numbers
write a loop that prints all the odd numbers between -3 and 19, -3, and 19 included.
List odd numbers in reverse order
write a loop that prints all the odd numbers starting at 9 and going down to 1, included.


Definite For-Loops that use Lists


  • We can use explicit lists to generate similar outputs. For example, the output of the first two examples in the previous section could have been generated by the following loops:


>>>for n in [0, 1, 2, 3]:
            print( n )

>>>for n in [1, 2, 3]:
            print( n )



Challenge #2: List-Controlled Loops

QuestionMark2.jpg


Solve the 3 problems of Challenge 1 above, using lists instead of the range() function.








For-Loops ready for modification


  • Create the following python program (we're not working in interactive mode at this point). Don't hesitate to copy and paste to go faster!
# lab2seq.py
# 
 
# print the contents of the farm
for animal in  [ "dog", "cat", "horse" ]:
     print animal


  • Add a pig to your list of animals, and make sure it gets listed by the program.


Modification #1
Change the for-loop of your program so that it reads:


       counter = 0
       for animal in  [ "dog", "cat", "horse", "pig" ]:
           print( counter, animal )
           counter = counter + 1


  • Run the program. Observe its output. From this observation, modify the program so that its output now reads something like this (user input is in underlined):
       Your farm contains
       animal # 1 : a sheep
       animal # 2 : a dog
       animal # 3 : a bear
       animal # 4 : a mouse

Old MacDonald's Farm

Old MacDonald's farm is a song most American kids learn at one point in their life. I certainly never heard it when I grew up in France, but I think most of you will be familiar with it.

If you don't know it, this YouTube video will get you acquainted with it. :-)

The goal of this problem is for you to add a section to the program below so that it prints parts of the lyrics of the famous song using a for-loop.

Here's the beginning program which you have to modify:

     
# display the names of the animals
for animal in  [ "horse", "pig", "dog", "cat" ]:
      print animal


First modification

  • Modify this program so that it uses the loop, and outputs the lyrics of the song:
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a horse E-I-E-I-O
    Here a horse, there a horse, everywhere a horse !
    
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a pig E-I-E-I-O
    Here a pig, there a pig, everywhere a pig !
    
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a dog E-I-E-I-O
    Here a dog, there a dog, everywhere a dog !
    
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a cat E-I-E-I-O
    Here a cat, there a cat, everywhere a cat !

(don't worry if you get extra spaces in your output)

The input() Function


Try the following statements in the console window. Again, some of these might generate errors. It is important for you to understand why these errors occur.


>>> firstName = input( "Enter your first name: " )
>>> firstName

>>> lastName = input( "Enter your last name: " )
>>> lastName

>>> print( firstName, lastName )

>>> x = input( "enter a number: " )
>>> x

>>> x + 5

>>> x = input( "Enter a number: " )
>>> x

>>> x = eval( answer )
>>> x
>>> x + 5

>>> y = eval(  "314.56789" )
>>> y



Challenge #4: Gathering student information

QuestionMark4.jpg


Write a python program that asks the user for her name, box number, 99-Id number, and phone number, and that outputs it back on the screen nicely formatted.

Example (the information entered by the user is in boldface):

your first name? Allie
your last name? Gator
your box number? 1234
your phone number? 413 456 7890
your Smith Id? 990123456


Name:   Allie Gator (990123456)
box #:  1234
phone:  413 456 7890



Challenge #5: (Challenging) Printing the student information it in a box

QuestionMark5.jpg


Same idea as with the previous challenge but after inputing the information from the user, the program outputs it in a box!

Example:

Lab2 2014 boxedOutput.png





Challenge #7: Seconds to Days, Hours, Minutes, Second converter

QuestionMark8.jpg


Write a program that prompts the user for a time expressed as a number of seconds. The program then outputs the equivalent number of days, hours, minutes, and seconds.

Make the program output the result in a box, similar to the one shown below (the user input is in boldface):

Please enter a time expressed in seconds: 3662


+--------------------------------------------+
| 0 day(s) 1 hour(s) 1 minute(s) 2 second(s) |
+--------------------------------------------+