Difference between revisions of "CSC111 Lab 2 2015"
(→Printing programs from Linux) |
(→Single for-loops) |
||
Line 9: | Line 9: | ||
+ | |||
+ | =The '''input()''' Function= | ||
+ | <br /> | ||
+ | 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. | ||
+ | |||
+ | <br /> | ||
+ | <code><pre> | ||
+ | >>> 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 = int( answer ) | ||
+ | >>> x | ||
+ | |||
+ | >>> y = int( "314.56789" ) | ||
+ | >>> y | ||
+ | </pre></code> | ||
+ | <br /> | ||
+ | |||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge #4: Gathering student information== | ||
+ | |} | ||
+ | [[Image:QuestionMark4.jpg|right|120px]] | ||
+ | <br /> | ||
+ | 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 | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge #5: (Challenging) Printing the student information it in a box== | ||
+ | |} | ||
+ | [[Image:QuestionMark5.jpg|right|120px]] | ||
+ | <br /> | ||
+ | Same idea as with the previous challenge but after inputing the information from the user, the program outputs it in a box! | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <center>[[Image:Lab2_2014_boxedOutput.png]]</center> | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge #6: Programmable ATM Machine== | ||
+ | |} | ||
+ | [[Image:QuestionMark6.jpg|right|120px]] | ||
+ | <br /> | ||
+ | Modify your last ATM program so that it prompts the user for an amount of money to withdraw, and then it breaks it down into number of bills of different denominations. | ||
+ | <br /> | ||
+ | Here is an example with the user input in boldface: | ||
+ | |||
+ | |||
+ | Please enter the amount you want to withdraw: '''231.50''' | ||
+ | Please lift you keyboard and find: | ||
+ | ... | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge #7: Seconds to Days, Hours, Minutes, Second converter== | ||
+ | |} | ||
+ | [[Image:QuestionMark8.jpg|right|120px]] | ||
+ | <br /> | ||
+ | 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) | | ||
+ | +--------------------------------------------+ | ||
+ | |||
+ | <br /> | ||
=Single for-loops= | =Single for-loops= | ||
− | * For this section of the lab, open | + | * For this section of the lab, open IDLE and create the different program sections below in the edit window. |
* Create a new program called '''lab2.py''' | * Create a new program called '''lab2.py''' | ||
− | + | <br /> | |
− | + | <source lang="python"> | |
+ | # lab2.py | ||
− | + | for i in range( 3, 8 ): | |
− | + | print( i ) | |
− | |||
− | + | </source> | |
− | + | <br /> | |
* Verify that you get the correct output: | * Verify that you get the correct output: | ||
− | |||
− | |||
3 | 3 | ||
4 | 4 | ||
Line 38: | Line 149: | ||
;List of numbers, Version 2 | ;List of numbers, Version 2 | ||
− | : Modify your loop so that it does not use the ''range( )'' | + | : Modify your loop so that it does not use the ''range( )'' function (Hint: you have to list all the values, as we did in [[CSC111 Lab 1 2015|Lab #1]]. |
; List of even numbers: | ; List of even numbers: | ||
− | : write a loop that prints all the even numbers between 4 and 20 | + | : write a loop that prints all the even numbers between 4 and 20; 4, and 20 included (you can use range or list all the numbers). |
;List of odd numbers: | ;List of odd numbers: |
Revision as of 18:09, 3 February 2015
--D. Thiebaut (talk) 17:01, 3 February 2015 (EST)
Contents
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.
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 = int( answer )
>>> x
>>> y = int( "314.56789" )
>>> y
Challenge #4: Gathering student information |
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 |
Same idea as with the previous challenge but after inputing the information from the user, the program outputs it in a box!
Example:
Challenge #6: Programmable ATM Machine |
Modify your last ATM program so that it prompts the user for an amount of money to withdraw, and then it breaks it down into number of bills of different denominations.
Here is an example with the user input in boldface:
Please enter the amount you want to withdraw: 231.50 Please lift you keyboard and find: ...
Challenge #7: Seconds to Days, Hours, Minutes, Second converter |
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) | +--------------------------------------------+
Single for-loops
- For this section of the lab, open IDLE and create the different program sections below in the edit window.
- Create a new program called lab2.py
# lab2.py
for i in range( 3, 8 ):
print( i )
- Verify that you get the correct output:
3 4 5 6 7
- Now, your turn!
- List of numbers, Version 2
- Modify your loop so that it does not use the range( ) function (Hint: you have to list all the values, as we did in Lab #1.
- List of even numbers
- write a loop that prints all the even numbers between 4 and 20; 4, and 20 included (you can use range or list all the numbers).
- List of odd numbers
- write a loop that prints all the odd numbers between -3 and 19, -3, and 19 included.
- User-controlled loops (more difficult)
- Write a new Python program that prints a series of numbers, after having asked the user where the series should start, and where it should end.
- Here is an example of how it should run (the user input is iunderlined):
Python userLoop.py I will print a list of numbers for you. Where should I start? 3 What is the last number of the series? 10 3 4 5 6 7 8 9 10
- Go ahead and write it. What happens if you ask the program to count from -5 to 5? Does it work correctly?
- Does your program work if you ask your program to go from 1 to 1?
- Can you predict what will be printed if you ask your program to count from 10 to 1?
Lists and Operations on Lists
- Try the following Python statements in the Python shell (interactive mode).
>>> students = [ "alice", "kate", "renee"] >>> students >>> students.append( "monique" ) >>> students >>> students = [] >>> students >>> students = [ "alice", "jack" ] + [ "monique", "chris", "andrea" ] >>> students >>> students[ 0 ] >>> students[ 1 ] >>> students[ -1 ] >>> students[ -2 ] (try to figure out how negative indices work)
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 # # initialize the farm with animals farm = [ "dog", "cat", "horse" ] # print the contents of the farm for animal in farm: print animal
- Farm and animal are variables. One contains a list, the other one contains a string.
- Add a pig to your farm, and make sure it gets listed by the program.
- When you are done with the addition of the pig, observe another way for the program to do the same thing:
# lab2seq2.py # farm = [] # create an empty farm farm.append( "dog" ) # add a dog to it farm.append( "cat" ) # then a cat farm.append( "horse" ) # then a horse farm.append( "pig" ) # and a pig # print the contents of the farm for animal in farm: print animal
- Modification #1
- Edit this second program so that instead of always adding the same animals to the farm, it will get four names of animals from the user, and add them to the farm. It will then print the contents of the farm on the screen. (Hint: use raw_input)
- Modification #2
- Change the for-loop of your program so that it reads:
counter = 0 for animal in farm: 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):
python lab2seq3.py animal? sheep animal? dog animal? bear animal? mouse Your farm now 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 (almost) all 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:
def main(): # start with four animals in the farm farm = [ "horse", "pig", "dog", "cat" ] # display the names of the animals for animal in farm: print animal main()
First modification
- Modify this program so that the output looks like 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)
Second modification
- Modify your program some more, and make it prompt the user for the names of four animals first, and then make it display the lyrics as shown above with the user's selected animals.
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? 10 * * * * * * * * * * ! ! ! ! ! ! ! ! ! ! How many stars? 20 * * * * * * * * * * * * * * * * * * * * How many stars? 1 * ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !