This lab was written and designed to be completed during lab-time for cs250. This lab is not graded and does not need to be turned in. However, you should work through all of the problems, as it will help you on assignments and exams. If you cannot complete the lab in the alloted time, feel free to continue at home.
This lab is designed to get you comfortable using if-statements and while-loops.
Throughout many of the problems in this lab, we will be writing several python functions in the same file.
Create a new python file named lab5.py
and open it up in your editor of choice (atom).
Put all of the functions from all problems in this file as you go along.
To test out the functions, use the technique discussed in lab 4, where you import
the python file as a module.
Write a function named first_alpha_name
.
This function will take three arguments, which will be names.
You can call the arguments n1
, n2
and n3
.
This function will return the name that comes alphabetically “first” of the three.
In order to use this function, follow the same steps for re-loading the lab5
module described in lab 4.
Also, make sure to check that the function description is added to the help(lab5)
section.
Below are a few example runs:
$ python3.6
>>> import lab5
>>> lab5.first_alpha_name("Bill", "Jane", "Anne")
'Anne'
>>> lab5.first_alpha_name("Jimmy", "Kyle", "Paul")
'Jimmy'
>>> lab5.first_alpha_name("John", "Cameron", "Zach")
'Cameron'
>>>
Below is a CSV file with a listing of NBA players.
CLE, James, 23
CLE, Love, 0
MIA, James, 6
OKC, Durant, 35
GSW, Durant, 35
GSW, Curry, 30
MIN, Love, 42
MIN, Garnett, 21
BOS, Garnett, 5
NYK, Chandler, 6
PHX, Bledsoe, 2
PHX, Chandler, 4
The first column is the team name, the seconds is the (last) name of the player, and the third is the player’s jersey number.
In this problem, you are going to add a function named get_player_number
.
This function will take two arguments.
The first is a string representing the name of the team.
The second is the last name of the player.
The function should return
the number of the player on the given team.
You should not get the solution by reading the info from a CSV file (we have not covered that yet).
Use if-statements to figure out the correct value to return.
If a team/name combination is specified that the function does not support, return -1
, indicating an “invalid” jersey number.
Notice that both the team and the player name is necessary, because some players have played on multiple teams over the course of their career, and have had different numbers on the various teams.
$ python3.6
Python 3.6.0 (default, Dec 24 2016, 08:01:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lab5
>>> lab5.get_player_number('MIN', 'Love')
42
>>> lab5.get_player_number('CLE', 'Love')
0
>>> lab5.get_player_number('PHX', 'Bledsoe')
2
>>> lab5.get_player_number('MIN', 'Garnett')
21
>>> lab5.get_player_number('BOS', 'Garnett')
5
>>> lab5.get_player_number('CLE', 'James')
23
>>> lab5.get_player_number('PHX', 'James')
-1
>>> lab5.get_player_number('ABC', '???')
-1
>>>
This problem is the same as the one from lab 4, but you are not allowed to use any string multiplication to implement.
Instead, use while
loop(s).
Write another function in lab5.py
named rectangle
that prints a rectangle of characters.
This function will take three arguments.
The first is the width
of the rectangle,
The second is the height
of the rectangle.
The third is a character, which the rectangle will be made up of.
Write the function, add a descriptive comment immediately above it, and re-save lab5.py
.
In order to use this function, follow the same steps for re-loading the lab5
module described in lab 4.
Also, make sure to check that the function description is added to the help(lab5)
section.
HINT: You can use sys.stdout.write()
instead of print()
to print a string to standard output without a newline being automatically appended!
This may come in handy for this problem and the next.
Below are a few sample calls:
>>> lab5.rectangle(5, 3, '+')
+++++
+++++
+++++
>>> lab5.rectangle(38, 5, '#')
######################################
######################################
######################################
######################################
######################################
>>> lab5.rectangle(4, 15, 'O')
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
This problem is the same as the one from lab 4, but you must use at least two while
loops to implement it.
You may also use string multiplication, but it should work in concert with the loops.
Write another function in lab5.py
named checker_board
.
This function will print a checker board with the dimensions given by the user.
This function will take four arguments:
The first is the width/height of each tile on the board. This number should be a multiple of two! The second is the number of tiles along each edge. This number should also be a multiple of two! The third is the character to use for the “dark” tiles of the board. The fourth is the character to use for the “light” tiles of the board.
Write the function, add a descriptive comment immediately above it, and re-save lab5.py
.
In order to use this function, follow the same steps for re-loading the lab5
module described in lab 4.
Also, make sure to check that the function description is added to the help(lab5)
section.
Below are a few sample calls:
>>> lab5.checker_board(2, 4, '#', '-' )
##--##--
##--##--
--##--##
--##--##
##--##--
##--##--
--##--##
--##--##
>>> lab5.checker_board(1, 16, 'X', ' ' )
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
>>> lab5.checker_board(4, 4, '#', ' ' )
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
#### ####
>>> lab5.checker_board(4, 4, '-', '+' )
----++++----++++
----++++----++++
----++++----++++
----++++----++++
++++----++++----
++++----++++----
++++----++++----
++++----++++----
----++++----++++
----++++----++++
----++++----++++
----++++----++++
++++----++++----
++++----++++----
++++----++++----
++++----++++----
Once you have completed problems 3 and 4, compare you solution using while
loops to the solutions for the problems in lab 4 that did not use loops.
Write a python program named quiz.py
.
This program will be an interactive quiz, and will read user answers in from standard input.
If you forgot how to read lines from standard input, check out lecture notes 9.
This program will ask the user three questions, one by one. The user will type a response to each question on the command line, and then hit enter. Below are a few example runs of this quiz. You should be able to “reverse-engineer” the implementation of this quiz given the example runs below.
$ python3.6 quiz.py
Q1: What bash command is used to search for strings?
grep
CORRECT!
Q2: What bash command is used to list files?
ls
CORRECT!
Q3: What is Ben's favorite noodle?
spaetzle
CORRECT!
$
$ python3.6 quiz.py
Q1: What bash command is used to search for strings?
sed
WRONG! The correct answer is "grep"
Q2: What bash command is used to list files?
listing
WRONG! The correct answer is "ls"
Q3: What is Ben's favorite noodle?
bowtie
WRONG! The correct answer is "spaetzle"
$
$ python3.6 quiz.py
Q1: What bash command is used to search for strings?
I do not know
WRONG! The correct answer is "grep"
Q2: What bash command is used to list files?
ls
CORRECT!
Q3: What is Ben's favorite noodle?
linguine
WRONG! The correct answer is "spaetzle"
$