SlideShare a Scribd company logo
1 of 59
Skip To ContentOpen Quick LinksQuick Links
Page Landmarks
Content Outline
Keyboard Shortcuts Logout
Global Menu
Jerry ShenActivity Updates43 Home Help
Top Frame TabsHome
Tab 1 of 1 (active tab)
Current Location
Programming for Data Science (Fall 2019)
Module 2: Control Flow
Project 2: Playing the Lotto
Menu Management Options
Course Menu:
Programming for Data Science (Fall 2019)
AnnouncementsWELCOMEModule 0: Start here!
(Setup)RESOURCESSyllabusScheduleCourse FAQOffice and
Consulting HoursMy GradesEmail ProfessorCOURSE
MODULESModule 1: Variables and Data TypesModule 2:
Control FlowModule 3: Text Analysis with Python objects
My Groups CSCI140-01
Project 2: Playing the Lotto
Content
Project 2: Playing the Lotto
Attached Files:
Project_2_EC.py
(451 B) Project_2_Main.py
(114 B) Project2_vN14.pdf
(205.725 KB) Project_2.py
(1.226 KB)
For this project, you will create and correct a set of functions
that simulate playing the lottery, and you will write a main
program that makes use of your functions. The .pdf below
contains the specifications for the project. Skeleton python files
are provided in Project_2.py and Project_2_EC.py - you should
not delete any of the def lines from this file - if you do not fill
in a function definition, leave the word pass indented under that
definition, and Project_2_Main.py - this file is for your main
program.
Edited 9/23: You should not need to change order of lines for
debugging. You may need to change indentation.
Video Showing how the Project Files work
This video shows you how the two project files should look
(what they should and should not contain), how they work
together, and how to run the project from the command line.
Watch it - this is the #1 question about project 2, and the cause
of a lot of problems.
CSCI 140 Programming for Data Science Project 2
Checkpoint Deadline 1700, Sunday September 29
Due 1700, Friday, October 4
In our second project, you are tasked with developing functions
to simulate playing the lottery.
You will create two functions, correct a third function, and
write a short main program.
N.B.: You must use the structures we have learned in this class
to complete this assignment.
Implementations using structures we have not covered may
receive no credit. For example, you
may not use string and list methods since we have not formally
talked about methods.
We have provided two skeleton files: Project_2.py for your
function code, and
Project_2_Main.py for your main program.
Function One: Generating Lottery Numbers
Many states in the United States have a state lottery. Players
pick typically between 4 and 6
numbers within a certain range, and then win monetary prizes
based on how many of their
numbers match numbers randomly drawn in the lottery. For
example, a lottery could use the
numbers from 1 to 50, and players must pick 5 numbers. Each
number can only be chosen once,
that is, each number is unique. For example, a player could
choose: 39 7 12 14 42
Write a function called generate_numbers that randomly selects
lottery numbers. This function
has two optional arguments: limit – the maximum number used
in the lottery, i.e. the highest
number a player can choose, and num – the number of lottery
numbers to select. The default
value of limit is 34 and the default value of num is 5. These
defaults correspond to one of the
Virginia lottery games. Your function will return a list of
randomly selected lottery numbers. Here
are some examples – numbers are selected randomly so these
show only 1 possible outcome.
• generate_numbers with default values for limit and num
Example return value: [20, 10, 34, 12, 31]
• generate_numbers with default value for limit, num = 8:
Example return value: [16, 14, 12, 28, 26, 15, 30, 32]
• generate_numbers with limit = 55, num = 4:
Example return value [11, 26, 18, 48]
You can assume that the input for limit will always be a
positive integer greater than or equal to
num. The input for num will be a positive integer.
Key points:
• Optional argument limit gives the highest possible lottery
number that can be chosen;
inputs tested will always be correct type
• Each lottery number can only be chosen ONCE, they cannot be
repeated
• Optional argument num is an integer; default value: 5; inputs
tested always correct type
• Return a list of num integers randomly selected with no
duplicates between 1 and limit
• You can do this with a loop, but it is actually easier to use one
of the functions from the
random module without a loop; HINT: how can we generate the
numbers from 1 to limit?
Function Two:Win or Lose
To win the lottery, you must match your numbers to the winning
numbers. The prize you win
corresponds to how many of your numbers match the winning
numbers. The more numbers you
match, the larger the prize. For example, if your numbers are:
15 32 42 10 18 and the winning
numbers are: 32 42 15 18 9, you have matched four out of five
numbers and would win the second
largest prize. Typically, you don’t win a prize for matching very
few numbers, e.g. in the Virginia
lottery, you won’t get anything if you match only one or two
numbers to the winning numbers.
You have been code for a function called lotto_prize which
needs to be corrected. You may not
add or delete lines. You may change indentation, but should not
change the order of the lines.
The function lotto_prize takes three required arguments: attempt
– a list containing a player’s
lottery numbers, winning – the winning lottery numbers, and
prizes – a list containing the prize
amounts from largest to smallest. The function will return a
numeric value that is the prize won,
either an item from prizes or 0. Whether and how much the
player wins, and which output is
returned, is based on how many numbers match. Matching all
numbers gets the largest prize, all
but one the second largest, and so forth. If they match too few
numbers, they win nothing.
The examples below are for demonstration only, your function
should PRINT NOTHING.
• All of the player’s numbers match: player wins the largest
prize
attempt is [28, 49, 2, 55, 33, 44], winning is [2, 28, 49, 33, 44,
55]
prizes is [1000000, 400, 20, 5]
Function returns 1000000
• All but one of the player’s numbers match: player wins the
second largest prize
attempt is [28, 49, 2, 33, 44], winning is [2, 28, 9, 33, 44]
prizes is [1000, 40, 1]
Function returns 40
• Player matches too few numbers to win: player wins nothing
attempt is [28, 49, 2, 33, 44, 19, 4], winning is [50, 49, 27, 31,
2, 7, 11]
prizes is [1250000, 10000, 1500, 50]
Function returns 0
The inputs for attempt and winning will always be of the same
length, but that length can be
any positive integer. The input for prizes is a list sorted from
largest to smallest with a length less
than or equal to the length of attempt and winning. No other
inputs will be tested.
Key points:
• The text of the code you are given is ALMOST correct and
requires only minor changes.
These changes mostly have to do with logical errors and things
like indexing. You need to
preserve as much of the original code as possible. You may
need to change indentation
for some lines. You do not need to and are not permitted to add
any lines or delete
any lines. You must understand in detail what each line is meant
to do and how to solve
the problem on paper. READ THE LAST KEY POINT AND
COMMENTS IN CODE VERY
CAREFULLY.
• All of the required arguments are lists: attempt and winning
are the same length and are
unsorted; prizes has a length less than or equal to attempt and
winning and is sorted
largest to smallest - this means the user inputs it sorted, your
function does not sort it.
• Function returns an integer that is either the prize amount of
0; prize won depends on how
many numbers match
• The function is written to operate in this general manner:
check if each item from attempt
is part of winning, count how many items match. Use count of
matches to determine if
the player wins a prize, and if so, which prize from prizes they
win. Return prize amount.
Function Three: Can you really win?
You decide to play a set of lottery numbers until you win the
jackpot, that is until your numbers
match all of the winning numbers. How many times would you
have to play before this happens?
Write a function called simulate_lotto that takes two required
arguments: your_numbers –a
list containing your lottery numbers and max_pick – the highest
number available to pick in the
lottery – that is, the highest value that could appear in your
numbers or the winning numbers.
simulate_lotto also takes an optional argument max_plays which
has a default value of 10,000.
The purpose of max_plays is to prevent your code from running
forever. In a lottery with 6
numbers chosen from 1 to 50, there are 11,441,304,000 different
sets of winning numbers, which
doesn’t even account for the same numbers being chosen twice.
Your program could run for a
LONG time before the player’s numbers win. max_plays limits
how many times winning numbers
will be chosen. Even if the player hasn’t won, the function will
stop after max_plays lottery picks.
simulate_lotto should return an integer which is the number of
plays with your numbers it took
to win the jackpot –this means all of the numbers match- OR
max_plays, whichever is smaller.
Here are some examples to demonstrate how the function works.
This is NOT printed OUTPUT.
• Example #1: your_numbers is [45, 23, 12, 22], max_pick is
50, max_plays is 10,000
Play 1: winning numbers are [12, 49, 2, 45]
Play 2: winning numbers are [7, 8, 41, 13]
Play 3: winning numbers are [12, 23, 45, 22]
Function returns 3. Notice that the order of the numbers doesn’t
matter, just that the winning
numbers match your_numbers. Once they match, the function
terminates.
• Example #2: Example #1: your_numbers is [5, 2, 1], max_pick
is 10, max_plays is 4
Play 1: winning numbers are [1, 4, 2]
Play 2: winning numbers are [7, 8, 4]
Play 3: winning numbers are [1, 3, 4]
Play 4: winning numbers are [1,7,9]
Function returns 4 – the value of max_plays. Even though the
player didn’t win (winning numbers
were never [5,2,1]), the function terminates because the max
number of plays was reached.
Key points:
• Input for your_numbers will always be a list of integers. The
length of this list can vary.
• Input for max_pick will always be a positive integer greater
than or equal to the length
of your_numbers. Input for max_plays will always be a positive
integer.
• The winning numbers are generated by your function – they
are NOT input by the user.
You MUST use your generate_numbers function. Figure out
how many numbers to
generate from the user input your_numbers – HINT: look at the
length of the input.
• You MUST use your lotto_prize function to determine whether
or not the user won the
jackpot. Winning the jackpot means that all numbers match.
Think about how to
specify the prizes argument to make this work.
• You need to use a loop in this function. We have not discussed
break or continue. Do
not use them. Stop your loop either using logic or by exploiting
how return works.
MainProgram: Playing the Lottery
You will write a brief main program that makes use of your
functions. This must be saved and
submitted in Project_2_Main.py. The main program must make
use of your functions. You
will be graded specifically on coding style for this part of the
project – that means writing efficient
code that is not repetitive, and does not specifying default
arguments when possible. The main
program should do the following:
• Generate five lottery numbers to play where the numbers
range from 1 to 65. Use one of
your functions to do this.
• Generate a second set of five lottery numbers to be the
winning numbers. These also range
from 1 to 65. Use one of your functions to do this.
• Using the two sets of numbers you generated above, calculate
the prize the user wins. The
available prizes are: $100000 for all matching numbers, $500
for 4 matching numbers, and
$50 for 3 matching numbers. Use one of your functions to do
this. Print out the prize.
• Generate six lottery numbers to play where the numbers range
from 1 to 20. Use one of
your functions to do this. Simulate playing these numbers until
you win the jackpot or you
reach 1000 plays. Use one of your functions to do this. Print out
how many plays it took.
Extra Credit
Worth up to 3% total. Fill in under def lines in Project_2_EC.py
1) Many lotteries have a final number drawn that is a “super
pick” which has its own prizes and/or
can double/triple/etc. the prize won using the other numbers.
For example, you might pick 5
regular lottery numbers between 1 and 50 and then pick a final
number between 1 and 20:
45 2 5 14 29 Super pick: 11
Fill in the function definition for generate_numbers_super to
add support for a single extra
“super” pick. Keep in mind that the range of numbers for the
super pick may be different from the
range for the regular lottery numbers, and this function has an
additional parameter super_limit
which is the upper bound for the “super pick” (the highest
number the super pick can be). The
return value is a list where the LAST item in the list is the super
pick.
2) Fill in the function definition for lotto_prize_super to add
support for a super pick as described
in 1. This function has two additional arguments, s_num - a
Boolean indicating if the lottery
numbers provided indicate a super pick as the last number, and
multiplier – this indicates what
to multiply the prize by if the user wins. Here’s an example of
calling the function:
lotto_prize_super([43,57,23,45,1], [40, 45, 32,
23,1],[100000,100, 10], True, 2)
In this case, since s_num is True, 1 is separated from the other
numbers. The user matched two
regular numbers (45 and 23), so they would win $10. Since they
also matched the super pick and
multiplier is 2, this is doubled and the value returned is 20.
3) The corrected implementation of lotto_prize finds matches a
particular way. It might be more
efficient to determine matches by sorting the lists first and/or
implementing a search algorithm.
Or you could implement this function more concisely by using
objects other than lists. Fill in the
function definition for lotto_prize_ search with any
enhancements you can come up with.
SUBMISSION EXPECTATIONS
Project_2.py Your implementations of functions one, two
(corrected), and three. Do not change
the names of parameters in the function def lines or the names
of the functions – if you do so, it
will affect your grade. No additional code including input lines,
print lines, and function
calls should be submitted. Make sure that you have the correct
import lines. If your
code requires manual intervention to run such as adding/editing
import lines,
correcting indentation, removing print and input lines, etc.,
and/or correcting syntax
errors, you can expect up to a 5 point deduction.
Project_2_Main.py Your implementation of the main program
specified in Part 4. Your functions
must not be repeated in this file. They must be imported using
the correct import line. Make
sure that you have the correct import lines. If your code
requires manual intervention,
you can expect up to a 5 point deduction.
Project_2_EC.py Your implementations of any of the extra
credit functions. They must be
submitted in this file. No additional code including input lines,
print lines, and function
calls should be submitted. Make sure that you have the correct
import lines.
Project_2.pdf A PDF document containing your reflections on
the project. You must also cite
any sources you use. Please be aware that you can consult
sources, but all code
written must be your own. Programs copied in part or wholesale
from the web or other
sources will receive 0 points and will result in reporting of an
Honor Code violation.
N.B.: You must use the structures we have learned in this class
to complete this assignment.
Implementations using structures we have not covered may
receive no credit. This means NO LIST
AND STRING METHODS (yes, that includes append – do not
use it).
POINT VALUES AND GRADING RUBRIC
Function 1: 27.5 points
Function 2: 27.5 points
Function 3: 32.5 points
Main Program: 10 points
Write-up: 2.5 points
SUGGESTIONS FOR COMPLETION TIMELINE
This project integrates concepts from many lectures. The most
important step will be to
understand what each function does and figure out the necessary
steps. Make sure to work
things out on paper first. This schedule sets you up to submit all
functions for the checkpoint.
-Read through instructions and make sure you understand what
each function does and how it
works by 9/23 – bring questions to office hours and lab; start
writing steps on paper ASAP
-Complete debugging Function 2 after lecture 9/23-9/24
-Complete Function 1 and first three steps of main program
after lecture 9/25-9/26 (you could
complete this after lecture 9/23-9/24 but it will be easier with
material from 9/25-9/26)
-Complete Function 3 and last step of main program after
lecture 9/25-9/26 (you could complete
this after 9/23-9/24 but it will be easier with material from
9/25-9/26)
Traceback (most recent call last):
File "Prj2_test.py", line 3, in <module>
from Project_2 import *
ModuleNotFoundError: No module named ’Project_2’
$ 0
1000
$ 0
100
generate_numbers_super_defaults:
Generated [8, 12, 1, 27, 5, 14]
Checking min, max, length, and frequency
generate_numbers_super_equal:
Sample larger than population or is negative
Testing failed.
lotto_prize_search_jackpot_same:
attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10000
lotto_prize_search_jackpot_shuffle:
attempt: [5, 15, 2, 24, 56] winning: [24, 15, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10000
lotto_prize_search_jackpot_shuffle_2:
attempt: [5, 15, 2, 24, 56] winning: [24, 35, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 1000
lotto_prize_search_lose:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 0
lotto_prize_search_prize_odd:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes:
[10000, 1000, 100]
Return value was 0
lotto_prize_search_prize_odd_2:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 35, 13, 2, 11,
19, 42] prizes: [10000, 1000, 100]
Return value was 0
lotto_prize_prize_search_odd_3:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 15, 13, 2, 11,
19, 42] prizes: [10000, 1000, 100]
Return value was 0
lotto_prize_prize_search_odd_4:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 13, 2, 11,
19, 56] prizes: [10000, 1000, 100]
Return value was 100
lotto_prize_search_prize_odd_5:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11,
19, 56] prizes: [10000, 1000, 100]
Return value was 100
lotto_prize_search_prize_one:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11,
29, 56] prizes: [10000]
Return value was 0
lotto_prize_search_prize_one_2:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [2, 5, 19, 24, 77, 15,
56] prizes: [10000]
Return value was 10000
lotto_prize_search_shuffle_3:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 100
lotto_prize_search_shuffle_4:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10
lotto_prize_search_shuffle_5:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 1
lotto_prize_super_jackpot_same:
attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes:
[10000, 1000, 100, 10, 1]
local variable ’prize’ referenced before assignment
Testing failed.
lotto_prize_super_jackpot_same_2:
attempt: [5, 15, 2, 24, 56, 2] winning: [5, 15, 2, 24, 56, 2]
prizes: [10000, 1000, 100, 10, 1] Mult:4
Return value was 40000
lotto_prize_super_jackpot_shuffle:
attempt: [5, 15, 2, 24, 56, 6] winning: [24, 15, 56, 2, 5, 6]
prizes: [10000, 1000, 100, 10, 1] Mult:5
Return value was 50000
lotto_prize_super_lose:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes:
[10000, 1000, 100, 10]
Return value was 0
lotto_prize_super_shuffle_1:
attempt: [5, 15, 2, 24, 56, 32] winning: [44, 35, 56, 2, 11, 32]
prizes: [10000, 1000, 100, 10, 1] Mult:4
Return value was 40
Traceback (most recent call last):
File "Prj2_test.py", line 3, in <module>
from Project_2 import *
ModuleNotFoundError: No module named ’Project_2’
$ 0
1000
$ 0
100
generate_numbers_super_defaults:
Generated [8, 12, 1, 27, 5, 14]
Checking min, max, length, and frequency
generate_numbers_super_equal:
Sample larger than population or is negative
Testing failed.
lotto_prize_search_jackpot_same:
attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10000
lotto_prize_search_jackpot_shuffle:
attempt: [5, 15, 2, 24, 56] winning: [24, 15, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10000
lotto_prize_search_jackpot_shuffle_2:
attempt: [5, 15, 2, 24, 56] winning: [24, 35, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 1000
lotto_prize_search_lose:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 0
lotto_prize_search_prize_odd:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes:
[10000, 1000, 100]
Return value was 0
lotto_prize_search_prize_odd_2:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 35, 13, 2, 11,
19, 42] prizes: [10000, 1000, 100]
Return value was 0
lotto_prize_prize_search_odd_3:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 15, 13, 2, 11,
19, 42] prizes: [10000, 1000, 100]
Return value was 0
lotto_prize_prize_search_odd_4:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 13, 2, 11,
19, 56] prizes: [10000, 1000, 100]
Return value was 100
lotto_prize_search_prize_odd_5:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11,
19, 56] prizes: [10000, 1000, 100]
Return value was 100
lotto_prize_search_prize_one:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11,
29, 56] prizes: [10000]
Return value was 0
lotto_prize_search_prize_one_2:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [2, 5, 19, 24, 77, 15,
56] prizes: [10000]
Return value was 10000
lotto_prize_search_shuffle_3:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 100
lotto_prize_search_shuffle_4:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10
lotto_prize_search_shuffle_5:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 1
lotto_prize_super_jackpot_same:
attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes:
[10000, 1000, 100, 10, 1]
local variable ’prize’ referenced before assignment
Testing failed.
lotto_prize_super_jackpot_same_2:
attempt: [5, 15, 2, 24, 56, 2] winning: [5, 15, 2, 24, 56, 2]
prizes: [10000, 1000, 100, 10, 1] Mult:4
Return value was 40000
lotto_prize_super_jackpot_shuffle:
attempt: [5, 15, 2, 24, 56, 6] winning: [24, 15, 56, 2, 5, 6]
prizes: [10000, 1000, 100, 10, 1] Mult:5
Return value was 50000
lotto_prize_super_lose:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes:
[10000, 1000, 100, 10]
Return value was 0
lotto_prize_super_shuffle_1:
attempt: [5, 15, 2, 24, 56, 32] winning: [44, 35, 56, 2, 11, 32]
prizes: [10000, 1000, 100, 10, 1] Mult:4
Return value was 40
from Project_2_EC import *
from Project_21 import *
from random import randint, choice, choices, sample, shuffle
import sys
#Your main program goes here
#Main Program
#1
attempt = generate_numbers(65, 5)
#2
winning = generate_numbers(65, 5)
#3
prizes = [100000, 500, 50]
prize = lotto_prize(attempt,winning, prizes)
print("$", prize)
#4
max_pick = 20
max_play = 1000
winning_number = generate_numbers(20, 6)
plays = simulate_lotto(winning_number, max_pick, max_play)
print(plays)
#Extra Credits
#1
attempt = generate_numbers_super(65,5,20)
winning = generate_numbers_super(65,5,20)
prizes = [1000,100,10]
#2
prize = lotto_prize_super(attempt, winning, prizes, True, 5)
print("$", prize)
#3
print(lotto_prize_search([1,2,3,4,5],[1,2,3,4,6],[1000,100,10]))
from random import randint, choice, choices, sample, shuffle
from Project_21 import *
def generate_numbers_super(limit = 34, num = 5, super_limit =
20):
return sample(range(1,limit),num) + sample
(range(super_limit),1)
def lotto_prize_super(attempt, winning, prizes, super_ = False,
multiplier = 1):
if super_:
prize = lotto_prize(attempt, winning, prizes)
super_attempt = attempt[-1]
super_winning = winning[-1]
if super_attempt == super_winning:
prize *= multiplier
return prize
else:
return lotto_prize(attempt, winning, prize)
def lotto_prize_search(attempt, winning, prizes):
matches = len(set(attempt).intersection(winning))
count = len(winning)
for prize in prizes:
if(matches == count):
return prize
count = count - 1
return 0
#Absolutely no additional code in this file
#No function calls
#No input lines
#There should be no unindented code other than the import line
and the def lines
Hi Everyone,
Checkpoint results are now posted in Blackboard. Great work!
A few notes:
-Double check file names, import lines, and indentation.
-Check for infinite loops. If your file got stopped b/c of the
infinite loop in the debugging I commented or exited out of it so
you could still get results. But there were other student-created
infinite loops.
-No tests for Main Program. Some tests for EC.
-Unless you see the printed messaged "Testing failed" under a
test, your code passed the test
-All of the lotto prize tests show the inputs and your return
value - you can figure out the expected return value from the
inputs
-Double check you followed instructions ESPECIALLY for the
debugging
-Here's an example of a run with no errors with a few notes on
some tests:
generate_numbers_default_limit: #default for limit
Generated [12, 34, 32, 24, 8, 1, 18, 17]
Checking min, max, length, and frequency
.
generate_numbers_default_num: #default for num
Generated [7, 125, 143, 135, 139]
Checking min, max, length, and frequency
.
generate_numbers_defaults: #default for both
Generated [5, 3, 23, 24, 7]
Checking min, max, length, and frequency
.
generate_numbers_equal: #num and limit equal, forces every
number up to limit to be used
Generated [6, 4, 3, 8, 9, 2, 5, 10, 1, 7]
Checking min, max, length, and frequency
.
generate_numbers_rand_test: #checks randomness, selections
are only 1,2,3 - checks frequency of each possible combination
and for incorrect limits
Out of 60000 items,
9808 were [1,2,3]. Expected ~10000.
9975 were [2,1,3]. Expected ~10000.
9954 were [3,1,2]. Expected ~10000.
10097 were [1,3,2]. Expected ~10000.
10074 were [2,3,1]. Expected ~10000.
10092 were [3,2,1]. Expected ~10000.
0 did not match the specified options. THIS MUST BE 0.
.
lotto_prize_prize_even:
attempt: [5, 15, 2, 24] winning: [2, 25, 19, 24] prizes: [10000,
562]
Return value was 0
.
lotto_prize_prize_even_2:
attempt: [5, 15, 200, 24] winning: [200, 15, 19, 24] prizes:
[10000, 562]
Return value was 562
.
lotto_prize_jackpot_same:
attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10000
.
lotto_prize_jackpot_shuffle: #numbers out of order - all tests
that say shuffle
attempt: [5, 15, 2, 24, 56] winning: [24, 15, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10000
.
lotto_prize_jackpot_shuffle_2:
attempt: [5, 15, 2, 24, 56] winning: [24, 35, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 1000
.
lotto_prize_lose:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 0
.
lotto_prize_prize_odd:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes:
[10000, 1000, 100]
Return value was 0
.
lotto_prize_prize_odd_2:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 35, 13, 2, 11,
19, 42] prizes: [10000, 1000, 100]
Return value was 0
.
lotto_prize_prize_odd_3:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 15, 13, 2, 11,
19, 42] prizes: [10000, 1000, 100]
Return value was 0
.
lotto_prize_prize_odd_4:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 13, 2, 11,
19, 56] prizes: [10000, 1000, 100]
Return value was 100
.
lotto_prize_prize_odd_5:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11,
19, 56] prizes: [10000, 1000, 100]
Return value was 100
.
lotto_prize_prize_one:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11,
29, 56] prizes: [10000]
Return value was 0
.
lotto_prize_prize_one_2:
attempt: [5, 15, 2, 24, 56, 77, 19] winning: [2, 5, 19, 24, 77, 15,
56] prizes: [10000]
Return value was 10000
.
lotto_prize_shuffle_3:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 5] prizes:
[10000, 1000, 100, 10, 1]
Return value was 100
.
lotto_prize_shuffle_4:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 10
.
lotto_prize_shuffle_5:
attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes:
[10000, 1000, 100, 10, 1]
Return value was 1
.
simulate_lotto_max_plays: #forces max plays
Return value was 10
.
simulate_lotto_max_plays_large: #forces max plays
Return value was 1500
.
simulate_lotto_mult_MC: #based on Monte Carlo simulation -
runs the function many times and calcs average, if you failed
this, there is a small chance your code is OK - depends on
probability
On average, it took 9.8915 plays before you ran out of money.
.
simulate_lotto_single: #length of attempt and limit are the
same, so must win on first play, inputs are [1,2,3,4,5] and 5
Return value was 1
.
Project1/convert_age.py
Age_on_Earth=float(input('Enter your age on Earth: '))
days=[365.25,87.97,687,4331.87,10760.27,60189.55]
planet=int(input('Select a planet:1 for Merccury, 2 for Mars, 3
for Jupiter, 4 for Saturn, 5 for Neptune: '))
Age_on_planet=(365.25/days[planet])*Age_on_Earth
Types_of_planet=['Earth','Merccury','Mars','Jupiter','Saturn','Ne
ptune']
print('Your age on ', Types_of_planet[planet] , 'is
',Age_on_planet,' years')
Project1/convert_age_2.py
Home_planet=int(input('Select your home planet: 1 for
Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune:
'))
Age_on_home_planet=float(input('Enter your age on your home
planet: '))
Destination=int(input('Select your destinaion planet: 1 for
Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune:
'))
planet=[365,87.97,687,4331.87,10760.27,60189.55]
planets=['Earth','Mercury','Mars','Jupiter','Saturn','Neptune']
Age=(planet[Home_planet]/planet[Destination])*Age_on_home
_planet
print('Your age on',str(planets[Destination]),'is',Age,'years')
Project1/Earth_to_Mars.py
Earth_years = float(input('Enter the number of Earth years: '))
Earth_TO_Mars = 365/687
value = Earth_years*Earth_TO_Mars
print('This is',value,'years on Mars')
Project1/overdue_tax.py
PENALTY_RATE = 0.06 #Do not change
EARTH_YEAR = 365 #Do not change
tax = input('Enter amount of tax owed: ') #Do not change, will
be in format $100
rate = input('Enter interest rate: ') #Do not change, will be in
format 4%
late = int(input('Enter number of days overdue tax is: ')) #Do
not change, will be an integer
penalty = PENALTY_RATE * float(tax[1:])
interest = (float(rate[:-
1])/100)*(late/EARTH_YEAR)*float(tax[1:])
total = float(tax[1:])+penalty+interest
print('Your total payment is' , total)
Project1/Project1_v9.pdf
CSCI 140 Programming for Data Science Project 1
Due Friday, 13 September 2019 at 1700
Checkpoint Deadline: Tuesday, 10 September 2019 at 1700
In our first project, your task is to create 2 short programs,
complete one program, and correct a
fourth program. There are four programs to submit, each
performing one task. They can all be
completed using only the material introduced in the first couple
of days of class. You will submit
these are separate files with the extension .py, but you can write
and test your programs in a
notebook if you prefer. In the problem solving session, you will
practice creating, saving, and
running .py files.
We suggest that for each program you write an algorithm as a
simple numbered list of steps and
then create a Python implementation. Your submission will
consist of a PDF document containing
your reflections on the project, and four Python programs saved
in .py files named as specified.
Program One: Convert Earth Years to Mars Years
Elon Musk plans to retire on Mars. One year on Mars is 687
Earth days. One year on Earth is
365.25 Earth days. So, for example, if he stays on Mars for 16
Mars years, that’s about 30 Earth
years.
Write a program that solicits a single input from the user: a
number of Earth years. Your program
should convert the Earth years to Mars years using the
information given above.
You must format the program's interaction as illustrated in the
following examples. Do not vary
the phrasing, capitalization, spacing, etc. You must also write a
properly styled, easily readable
and understandable program.
Enter number of Earth years: 10
This is 5.316593886462882 years on Mars.
Enter number of Earth years: 0.5
This is 0.26582969432314413 years on Mars.
Your program must be called Earth_to_Mars.py and should
work for any non-negative numeric
input provided by the user. Other inputs will not be tested.
Program Two: Calculate Retirement Contributions
Speaking of retirement, you need to start saving early if you’re
going to make it to Mars. Many
employers offer workplace retirement accounts. Money is
deducted (taken out) of an individual’s
salary and is put into a separate account that is not accessible
(usually) until retirement.
Individuals get to select the amount they want to save per
paycheck.
Suppose that an individual wants to try to have the same amount
deducted from every paycheck
to hit a target for retirement savings. Many workplaces only
allow you to deduct money for
retirement as full dollar amounts, that is $50 as opposed to
$50.50. This means that the last
payment for the year will have to include a few extra dollars to
hit their savings target.
Here are some examples of the calculations – this is not code
and is not what your program’s
output should look like.
Example 1:
Target is $10000 and individual is paid 12 times per year
They save $833 dollars from the first 11 paychecks, which gives
a total of $9163, so they need to
save an extra $4 in the last paycheck, for a total of $837, to
reach the target of $10000.
(You might be wondering, why not just save $834 each
paycheck? The goal is not to overshoot
the target, because if you overshoot the IRS limit, you’ll find
yourself in a lot of trouble.)
Example 2:
Target is $3600 and individual is paid 24 times per year
In this case, they can save $150 from each paycheck and meet
the target exactly. There is no
extra money to deduct from the last paycheck.
Write a program that calculates the amount of each equal
deduction per paycheck, and how many
extra dollars should be added to the last payment to hit the
target. Your program will solicit two
inputs from the user: a target amount in whole dollars (no cents)
and the number of times an
individual is paid per year. Save this program in a file called
retirement.py.
Program execution should look like what is shown below. Pay
special attention to formatting,
spacing, and capitalization of the input and output lines.
Enter total amount to save: 10000
Enter number of paychecks per year: 12
You must make 11 deductions of $833 and one final deduction
of $837 to save $10000
Enter total amount to save: 3600
Enter number of paychecks per year: 24
You must make 23 deductions of $150 and one final deduction
of $150 to save $3600
Note that the output is the same whether or not the final
payment is a different value. You can
assume that the inputs for the target and number of paychecks
will be positive integers, and your
program should work for any such inputs.
Program Three: Fountain of Youth
In Program 1, you converted from years on Earth to years on
Mars. We now want to consider
other planets, and figure out what your age would be on other
planets, using their definition of a
year. For our purposes, a ‘year’ is the time it takes for a
particular planet to orbit the sun (a
revolution). The table below shows how long a year lasts on
other planets in terms of Earth days:
Mercury 87.97 Earth days
Mars 687 Earth days
Jupiter 4331.87 Earth days
Saturn 10760.27 Earth days
Neptune 60189.55 Earth days
Write a program to convert your age in Earth years to your age
in years on any of these 5 planets.
You have been given three lines of code that you cannot change
in any way which must be
used for your program in the file convert_age.py. The program
will take two inputs: an age in
Earth years and a number indicating which planet to use for the
conversion.
Program execution should look like what is shown below. Pay
special attention to formatting,
spacing, and capitalization of the input and output lines.
Enter your age on Earth: 80
Select a planet: 1 for Mercury, 2 for Mars, 3 for Jupiter, 4 for
Saturn, 5 for Neptune: 2
Your age on Mars is 42.53275109170306 years
Enter your age on Earth: 12.5
Select a planet: 1 for Mercury, 2 for Mars, 3 for Jupiter, 4 for
Saturn, 5 for Neptune: 1
Your age on Mercury is 51.89979538479027 years
You can assume that the inputs for age will always be positive
numbers, but they may have
fractional components. You can also assume that the inputs for
selecting a planet will always be
one of the integers 1,2,3,4, or 5. No other inputs will be tested.
N.B.: you must only use programming structures discussed so
far – this means no conditionals
and no imported mathematical functions/modules and no object
types that we have not discussed
yet. You don’t need them here.
Program Four: The Taxman
If you fail to pay income tax in a timely manner and you owe
the government (or the state you
live in) money, you are subject both to a penalty and interest on
the amount owed. Even if you
are living on Mars, if you are a US citizen, you have to file
Federal taxes.
Suppose that during your stay-cation on Mars, the interstellar
mail took too long and you haven’t
paid your taxes on time . . . your tax return was literally lost in
space. You owe the original tax
plus a penalty that is 6% of the tax amount – for our purposes
the penalty will always be 6%.
You also owe interest on the tax (not the penalty, just the tax).
The amount of interest depends
on the current yearly rate and the amount of interest you owe
depends on how many days overdue
the tax is. This is best shown by a few examples.
Example 1:
Tax owed: $500, 365 days (1 year) overdue
Penalty (6% of tax owed): $30
Interest rate: 5% yearly
Interest owed (5% of tax owed): $25
Total amount to pay: $555
Example 2:
Tax owed: $500, 1095 days (3 years) overdue
Penalty (6% of tax owed): $30
Interest rate: 4% yearly
Interest owed (4% of tax owed per year times 3 years): $60
Total amount to pay: $590
Example 3:
Tax owed: $500, 250 days overdue
Penalty (6% of tax owed): $30
Interest rate: 4% yearly
Interest owed (4% of tax owed per year times 250 days out of
365): approx.. $13.70
Total amount to pay: $543.70
In the file overdue_tax.py, you have been code for a program
that provides constants for the
length of a year and the penalty rate, and solicits the tax owed,
the number of days overdue, and
the interest rate from the user. The program is supposed to
calculate the total amount owed, but
the program is incorrect. Your job is to debug the program.
Debugging the code means
that you edit the lines in place. You should not add lines, delete
lines, or move the order
of lines. Correct the lines as they are and preserve as much of
the original code as
possible. HINT: figure out what each line is trying to
accomplish first before making any
changes.
The user (and our testing program) will enter the data in the
following formats: tax owed will be
a string that starts with a dollar sign, e.g., $10000 and interest
rate will be a string that is a
percentage with a percent sign, e.g. 5%. The days overdue will
be entered as an integer.
Here are some examples of program execution. Pay special
attention to the formats of the inputs
and outputs – your printed output should match exactly in terms
of spacing and text, but your
output may have slightly different numbers of decimal places or
differences in the decimal portion
and that is OK (e.g. 543.6986301369863 versus
543.6986301369864):
Enter amount of tax owed: $500
Enter interest rate: 5%
Enter number of days overdue tax is: 365
Your total payment is 555.0
Enter amount of tax owed: $500
Enter interest rate: 4%
Enter number of days overdue tax is: 1095
Your total payment is 590.0
Enter amount of tax owed: $500
Enter interest rate: 4%
Enter number of days overdue tax is: 250
Your total payment is 543.6986301369863
Your program should work for any inputs in the format shown –
inputs representing numeric values
will always be positive numbers, but keep in mind that tax owed
may not be in exact dollar values
(i.e. there may be cents like $15000.25 – the IRS usually rounds
to the nearest dollar but we
won’t) and the interest percentage may not be a whole number
and may have more than a single
digit (e.g. 10.5%). The number of days will always be input as a
positive integer.
N.B.: you must only use programming structures discussed so
far – this means no conditionals
and no imported mathematical functions/modules and no object
types that we have not discussed
yet. You don’t need them.
Extra credit opportunities:
Please note that the following tasks are worth a small
percentage of extra credit (up to 3%).
1) Program 4 produces outputs that are dollar amounts that may
include cents. Format the
output so that it prints with two decimal places exactly. It is
find to round the second
decimal place based on the value of the third decimal place.
You may import any
modules and use any functions you like to accomplish this. Save
this program in a
separate file with the name overdue_tax_2.py
2) Write a modified version of Program 3 that allows the user to
input their age on a home
planet and then converts their age to the selected destination
planet. The program will
provide the output in the requested format. You must implement
this WITHOUT using
conditionals. Save this as convert_age_2.py. Here is an of how
this should work. This
example doesn’t include Earth as an option, but you can include
it if you like.
Select your home planet: 1 for Mercury, 2 for Mars, 3 for
Jupiter, 4 for Saturn, 5 for Neptune: 1
Enter your age on your home planet: 10
Select a destination planet: 1 for Mercury, 2 for Mars, 3 for
Jupiter, 4 for Saturn, 5 for Neptune:2
Your age on Mars is 1.280494905385735 years
SUBMISSION EXPECTATIONS
Project1.pdf A PDF document containing your reflections on the
project, and information on any
extra credit opportunities you pursued. You must also cite any
sources you use. Please be
aware that you can consult sources, but all code written must be
your own. Programs
copied in part or wholesale from the web or other sources will
receive 0 points and will
result in reporting of an Honor Code violation.
Earth_to_Mars.py Your implementation of program one,
following the format specified above.
retirement.py Your implementation of program two, following
the format specified above.
convert_age.py Your completed implementation of program
three.
overdue_tax.py Your corrected implementation of program four.
Any programs you attempted for extra credit as separate files.
Checkpoint submission
You may submit any and all parts of the project by Tuesday
September 10 to receive feedback
from our automated testing script. This is automated feedback
only but will show you if your
programs have failed any of our test cases. Please be aware that
you will receive no
feedback if your files are incorrectly named or are in an
incorrect format.
SUGGESTED COMPLETION SCHEDULE
Each project will offer you a suggest completion schedule to
help you manage your time.
Working incrementally is essential to learning the material,
minimizing frustration, and finishing
projects by the deadline. This schedule is based on when topics
are presented in lecture. This
will put you on track to have versions of 3 of the 4 programs to
submit by the checkpoint
deadline.
Earth_to_Mars.py: Complete after lecture 9/2 or 9/3
retirement.py: Start after lecture 9/2 or 9/3, Complete after
lecture 9/4 or 9/5
convert_age.py: Complete after lecture 9/9 or 9/10
overdue_tax.py: Complete after lecture 9/4 or 9/5
POINT VALUES AND GRADING RUBRIC
Program 1: 15 points
Program 2: 27.5 points
Program 3: 27.5 points
Program 4: 27.5 points
Write-up: 2.5 points
The general grading rubric for projects is below. Some of the
specifics given in the rubric are not
applicable to this project (e.g. import lines and copious
commenting).
Project1/retirement.py
amount_to_save = int(input('Enter total amount to save: '))
paychecks = int(input('Enter number of paychecks per year: '))
deduction_times = int(paychecks -1)
deduction = amount_to_save//paychecks
final_deduction = amount_to_save - deduction_times*deduction
print('You must make '+str(deduction_times)+' deductions of
$'+str(deduction)+' and one final deduction of
$'+str(final_deduction)+' to save $'+str(amount_to_save))
Project1/Write up.doc
Project1/Write up.pdf
CS140
Professor Willner
Wenda Shen
In this project, I finished the reading assignment and reviewed
the class notes, so
that I can attempt the project by using the knowledge we
already learned. So far I
didn’t look for anything on internet in order to finish the
project. This project helps
me learn how to differentiate int, float, and strings, and how to
refer to a certain
position in a list. Moreover, I also learned how to use the
correct format to avoid
error.
CSCI 140 Programming for Data Science Project 2
Checkpoint Deadline 1700, Sunday September 29
Due 1700, Friday, October 4
In our second project, you are tasked with developing functions
to simulate playing the lottery.
You will create two functions, correct a third function, and
write a short main program.
N.B.: You must use the structures we have learned in this class
to complete this assignment.
Implementations using structures we have not covered may
receive no credit. For example, you
may not use string and list methods since we have not formally
talked about methods.
We have provided two skeleton files: Project_2.py for your
function code, and
Project_2_Main.py for your main program.
Function One: Generating Lottery Numbers
Many states in the United States have a state lottery. Players
pick typically between 4 and 6
numbers within a certain range, and then win monetary prizes
based on how many of their
numbers match numbers randomly drawn in the lottery. For
example, a lottery could use the
numbers from 1 to 50, and players must pick 5 numbers. Each
number can only be chosen once,
that is, each number is unique. For example, a player could
choose: 39 7 12 14 42
Write a function called generate_numbers that randomly selects
lottery numbers. This function
has two optional arguments: limit – the maximum number used
in the lottery, i.e. the highest
number a player can choose, and num – the number of lottery
numbers to select. The default
value of limit is 34 and the default value of num is 5. These
defaults correspond to one of the
Virginia lottery games. Your function will return a list of
randomly selected lottery numbers. Here
are some examples – numbers are selected randomly so these
show only 1 possible outcome.
• generate_numbers with default values for limit and num
Example return value: [20, 10, 34, 12, 31]
• generate_numbers with default value for limit, num = 8:
Example return value: [16, 14, 12, 28, 26, 15, 30, 32]
• generate_numbers with limit = 55, num = 4:
Example return value [11, 26, 18, 48]
You can assume that the input for limit will always be a
positive integer greater than or equal to
num. The input for num will be a positive integer.
Key points:
• Optional argument limit gives the highest possible lottery
number that can be chosen;
inputs tested will always be correct type
• Each lottery number can only be chosen ONCE, they cannot be
repeated
• Optional argument num is an integer; default value: 5; inputs
tested always correct type
• Return a list of num integers randomly selected with no
duplicates between 1 and limit
• You can do this with a loop, but it is actually easier to use one
of the functions from the
random module without a loop; HINT: how can we generate the
numbers from 1 to limit?
Function Two:Win or Lose
To win the lottery, you must match your numbers to the winning
numbers. The prize you win
corresponds to how many of your numbers match the winning
numbers. The more numbers you
match, the larger the prize. For example, if your numbers are:
15 32 42 10 18 and the winning
numbers are: 32 42 15 18 9, you have matched four out of five
numbers and would win the second
largest prize. Typically, you don’t win a prize for matching very
few numbers, e.g. in the Virginia
lottery, you won’t get anything if you match only one or two
numbers to the winning numbers.
You have been code for a function called lotto_prize which
needs to be corrected. You may not
add or delete lines. You may change indentation, but should not
change the order of the lines.
The function lotto_prize takes three required arguments: attempt
– a list containing a player’s
lottery numbers, winning – the winning lottery numbers, and
prizes – a list containing the prize
amounts from largest to smallest. The function will return a
numeric value that is the prize won,
either an item from prizes or 0. Whether and how much the
player wins, and which output is
returned, is based on how many numbers match. Matching all
numbers gets the largest prize, all
but one the second largest, and so forth. If they match too few
numbers, they win nothing.
The examples below are for demonstration only, your function
should PRINT NOTHING.
• All of the player’s numbers match: player wins the largest
prize
attempt is [28, 49, 2, 55, 33, 44], winning is [2, 28, 49, 33, 44,
55]
prizes is [1000000, 400, 20, 5]
Function returns 1000000
• All but one of the player’s numbers match: player wins the
second largest prize
attempt is [28, 49, 2, 33, 44], winning is [2, 28, 9, 33, 44]
prizes is [1000, 40, 1]
Function returns 40
• Player matches too few numbers to win: player wins nothing
attempt is [28, 49, 2, 33, 44, 19, 4], winning is [50, 49, 27, 31,
2, 7, 11]
prizes is [1250000, 10000, 1500, 50]
Function returns 0
The inputs for attempt and winning will always be of the same
length, but that length can be
any positive integer. The input for prizes is a list sorted from
largest to smallest with a length less
than or equal to the length of attempt and winning. No other
inputs will be tested.
Key points:
• The text of the code you are given is ALMOST correct and
requires only minor changes.
These changes mostly have to do with logical errors and things
like indexing. You need to
preserve as much of the original code as possible. You may
need to change indentation
for some lines. You do not need to and are not permitted to add
any lines or delete
any lines. You must understand in detail what each line is meant
to do and how to solve
the problem on paper. READ THE LAST KEY POINT AND
COMMENTS IN CODE VERY
CAREFULLY.
• All of the required arguments are lists: attempt and winning
are the same length and are
unsorted; prizes has a length less than or equal to attempt and
winning and is sorted
largest to smallest - this means the user inputs it sorted, your
function does not sort it.
• Function returns an integer that is either the prize amount of
0; prize won depends on how
many numbers match
• The function is written to operate in this general manner:
check if each item from attempt
is part of winning, count how many items match. Use count of
matches to determine if
the player wins a prize, and if so, which prize from prizes they
win. Return prize amount.
Function Three: Can you really win?
You decide to play a set of lottery numbers until you win the
jackpot, that is until your numbers
match all of the winning numbers. How many times would you
have to play before this happens?
Write a function called simulate_lotto that takes two required
arguments: your_numbers –a
list containing your lottery numbers and max_pick – the highest
number available to pick in the
lottery – that is, the highest value that could appear in your
numbers or the winning numbers.
simulate_lotto also takes an optional argument max_plays which
has a default value of 10,000.
The purpose of max_plays is to prevent your code from running
forever. In a lottery with 6
numbers chosen from 1 to 50, there are 11,441,304,000 different
sets of winning numbers, which
doesn’t even account for the same numbers being chosen twice.
Your program could run for a
LONG time before the player’s numbers win. max_plays limits
how many times winning numbers
will be chosen. Even if the player hasn’t won, the function will
stop after max_plays lottery picks.
simulate_lotto should return an integer which is the number of
plays with your numbers it took
to win the jackpot –this means all of the numbers match- OR
max_plays, whichever is smaller.
Here are some examples to demonstrate how the function works.
This is NOT printed OUTPUT.
• Example #1: your_numbers is [45, 23, 12, 22], max_pick is
50, max_plays is 10,000
Play 1: winning numbers are [12, 49, 2, 45]
Play 2: winning numbers are [7, 8, 41, 13]
Play 3: winning numbers are [12, 23, 45, 22]
Function returns 3. Notice that the order of the numbers doesn’t
matter, just that the winning
numbers match your_numbers. Once they match, the function
terminates.
• Example #2: Example #1: your_numbers is [5, 2, 1], max_pick
is 10, max_plays is 4
Play 1: winning numbers are [1, 4, 2]
Play 2: winning numbers are [7, 8, 4]
Play 3: winning numbers are [1, 3, 4]
Play 4: winning numbers are [1,7,9]
Function returns 4 – the value of max_plays. Even though the
player didn’t win (winning numbers
were never [5,2,1]), the function terminates because the max
number of plays was reached.
Key points:
• Input for your_numbers will always be a list of integers. The
length of this list can vary.
• Input for max_pick will always be a positive integer greater
than or equal to the length
of your_numbers. Input for max_plays will always be a positive
integer.
• The winning numbers are generated by your function – they
are NOT input by the user.
You MUST use your generate_numbers function. Figure out
how many numbers to
generate from the user input your_numbers – HINT: look at the
length of the input.
• You MUST use your lotto_prize function to determine whether
or not the user won the
jackpot. Winning the jackpot means that all numbers match.
Think about how to
specify the prizes argument to make this work.
• You need to use a loop in this function. We have not discussed
break or continue. Do
not use them. Stop your loop either using logic or by exploiting
how return works.
MainProgram: Playing the Lottery
You will write a brief main program that makes use of your
functions. This must be saved and
submitted in Project_2_Main.py. The main program must make
use of your functions. You
will be graded specifically on coding style for this part of the
project – that means writing efficient
code that is not repetitive, and does not specifying default
arguments when possible. The main
program should do the following:
• Generate five lottery numbers to play where the numbers
range from 1 to 65. Use one of
your functions to do this.
• Generate a second set of five lottery numbers to be the
winning numbers. These also range
from 1 to 65. Use one of your functions to do this.
• Using the two sets of numbers you generated above, calculate
the prize the user wins. The
available prizes are: $100000 for all matching numbers, $500
for 4 matching numbers, and
$50 for 3 matching numbers. Use one of your functions to do
this. Print out the prize.
• Generate six lottery numbers to play where the numbers range
from 1 to 20. Use one of
your functions to do this. Simulate playing these numbers until
you win the jackpot or you
reach 1000 plays. Use one of your functions to do this. Print out
how many plays it took.
Extra Credit
Worth up to 3% total. Fill in under def lines in Project_2_EC.py
1) Many lotteries have a final number drawn that is a “super
pick” which has its own prizes and/or
can double/triple/etc. the prize won using the other numbers.
For example, you might pick 5
regular lottery numbers between 1 and 50 and then pick a final
number between 1 and 20:
45 2 5 14 29 Super pick: 11
Fill in the function definition for generate_numbers_super to
add support for a single extra
“super” pick. Keep in mind that the range of numbers for the
super pick may be different from the
range for the regular lottery numbers, and this function has an
additional parameter super_limit
which is the upper bound for the “super pick” (the highest
number the super pick can be). The
return value is a list where the LAST item in the list is the super
pick.
2) Fill in the function definition for lotto_prize_super to add
support for a super pick as described
in 1. This function has two additional arguments, s_num - a
Boolean indicating if the lottery
numbers provided indicate a super pick as the last number, and
multiplier – this indicates what
to multiply the prize by if the user wins. Here’s an example of
calling the function:
lotto_prize_super([43,57,23,45,1], [40, 45, 32,
23,1],[100000,100, 10], True, 2)
In this case, since s_num is True, 1 is separated from the other
numbers. The user matched two
regular numbers (45 and 23), so they would win $10. Since they
also matched the super pick and
multiplier is 2, this is doubled and the value returned is 20.
3) The corrected implementation of lotto_prize finds matches a
particular way. It might be more
efficient to determine matches by sorting the lists first and/or
implementing a search algorithm.
Or you could implement this function more concisely by using
objects other than lists. Fill in the
function definition for lotto_prize_ search with any
enhancements you can come up with.
SUBMISSION EXPECTATIONS
Project_2.py Your implementations of functions one, two
(corrected), and three. Do not change
the names of parameters in the function def lines or the names
of the functions – if you do so, it
will affect your grade. No additional code including input lines,
print lines, and function
calls should be submitted. Make sure that you have the correct
import lines. If your
code requires manual intervention to run such as adding/editing
import lines,
correcting indentation, removing print and input lines, etc.,
and/or correcting syntax
errors, you can expect up to a 5 point deduction.
Project_2_Main.py Your implementation of the main program
specified in Part 4. Your functions
must not be repeated in this file. They must be imported using
the correct import line. Make
sure that you have the correct import lines. If your code
requires manual intervention,
you can expect up to a 5 point deduction.
Project_2_EC.py Your implementations of any of the extra
credit functions. They must be
submitted in this file. No additional code including input lines,
print lines, and function
calls should be submitted. Make sure that you have the correct
import lines.
Project_2.pdf A PDF document containing your reflections on
the project. You must also cite
any sources you use. Please be aware that you can consult
sources, but all code
written must be your own. Programs copied in part or wholesale
from the web or other
sources will receive 0 points and will result in reporting of an
Honor Code violation.
N.B.: You must use the structures we have learned in this class
to complete this assignment.
Implementations using structures we have not covered may
receive no credit. This means NO LIST
AND STRING METHODS (yes, that includes append – do not
use it).
POINT VALUES AND GRADING RUBRIC
Function 1: 27.5 points
Function 2: 27.5 points
Function 3: 32.5 points
Main Program: 10 points
Write-up: 2.5 points
SUGGESTIONS FOR COMPLETION TIMELINE
This project integrates concepts from many lectures. The most
important step will be to
understand what each function does and figure out the necessary
steps. Make sure to work
things out on paper first. This schedule sets you up to submit all
functions for the checkpoint.
-Read through instructions and make sure you understand what
each function does and how it
works by 9/23 – bring questions to office hours and lab; start
writing steps on paper ASAP
-Complete debugging Function 2 after lecture 9/23-9/24
-Complete Function 1 and first three steps of main program
after lecture 9/25-9/26 (you could
complete this after lecture 9/23-9/24 but it will be easier with
material from 9/25-9/26)
-Complete Function 3 and last step of main program after
lecture 9/25-9/26 (you could complete
this after 9/23-9/24 but it will be easier with material from
9/25-9/26)

More Related Content

Similar to Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx

Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchartfika sweety
 
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxWeek 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxmelbruce90096
 
Casestudy 01 algorithms
Casestudy 01 algorithmsCasestudy 01 algorithms
Casestudy 01 algorithmsrawaha134
 
CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021
CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021
CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021MargenePurnell14
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018Robert Geofroy
 
2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problemFrankie Jones
 
goalseekandsensitivityanalysis-221112123352-9fe0067e.pptx
goalseekandsensitivityanalysis-221112123352-9fe0067e.pptxgoalseekandsensitivityanalysis-221112123352-9fe0067e.pptx
goalseekandsensitivityanalysis-221112123352-9fe0067e.pptxIrfanRashid36
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICAemtrajano
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comBaileya126
 
problem solving and design By ZAK
problem solving and design By ZAKproblem solving and design By ZAK
problem solving and design By ZAKTabsheer Hasan
 
Intro to programing with java-lecture 3
Intro to programing with java-lecture 3Intro to programing with java-lecture 3
Intro to programing with java-lecture 3Mohamed Essam
 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxNishaRohit6
 
Comp 122 lab 6 lab report and source code
Comp 122 lab 6 lab report and source codeComp 122 lab 6 lab report and source code
Comp 122 lab 6 lab report and source codepradesigali1
 
As with all projects in this course, your program’s output wil.docx
As with all projects in this course, your program’s output wil.docxAs with all projects in this course, your program’s output wil.docx
As with all projects in this course, your program’s output wil.docxrandymartin91030
 

Similar to Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx (18)

Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchart
 
003.query
003.query003.query
003.query
 
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxWeek 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
 
Casestudy 01 algorithms
Casestudy 01 algorithmsCasestudy 01 algorithms
Casestudy 01 algorithms
 
CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021
CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021
CSCE 1030 Project 2 Due 1159 PM on Sunday, March 28, 2021
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018
 
2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem
 
goalseekandsensitivityanalysis-221112123352-9fe0067e.pptx
goalseekandsensitivityanalysis-221112123352-9fe0067e.pptxgoalseekandsensitivityanalysis-221112123352-9fe0067e.pptx
goalseekandsensitivityanalysis-221112123352-9fe0067e.pptx
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICA
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Print9
Print9Print9
Print9
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.com
 
problem solving and design By ZAK
problem solving and design By ZAKproblem solving and design By ZAK
problem solving and design By ZAK
 
Intro to programing with java-lecture 3
Intro to programing with java-lecture 3Intro to programing with java-lecture 3
Intro to programing with java-lecture 3
 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptx
 
Comp 122 lab 6 lab report and source code
Comp 122 lab 6 lab report and source codeComp 122 lab 6 lab report and source code
Comp 122 lab 6 lab report and source code
 
Lecture 05.pptx
Lecture 05.pptxLecture 05.pptx
Lecture 05.pptx
 
As with all projects in this course, your program’s output wil.docx
As with all projects in this course, your program’s output wil.docxAs with all projects in this course, your program’s output wil.docx
As with all projects in this course, your program’s output wil.docx
 

More from edgar6wallace88877

Write a page to a page and half for each topic and read each topic a.docx
Write a page to a page and half for each topic and read each topic a.docxWrite a page to a page and half for each topic and read each topic a.docx
Write a page to a page and half for each topic and read each topic a.docxedgar6wallace88877
 
Write a page discussing why you believe PMI is focusing BA as the fi.docx
Write a page discussing why you believe PMI is focusing BA as the fi.docxWrite a page discussing why you believe PMI is focusing BA as the fi.docx
Write a page discussing why you believe PMI is focusing BA as the fi.docxedgar6wallace88877
 
Write a page of personal reflection of your present leadership compe.docx
Write a page of personal reflection of your present leadership compe.docxWrite a page of personal reflection of your present leadership compe.docx
Write a page of personal reflection of your present leadership compe.docxedgar6wallace88877
 
Write a page of compare and contrast for the Big Five Personalit.docx
Write a page of compare and contrast for the Big Five Personalit.docxWrite a page of compare and contrast for the Big Five Personalit.docx
Write a page of compare and contrast for the Big Five Personalit.docxedgar6wallace88877
 
Write a page of research and discuss an innovation that includes mul.docx
Write a page of research and discuss an innovation that includes mul.docxWrite a page of research and discuss an innovation that includes mul.docx
Write a page of research and discuss an innovation that includes mul.docxedgar6wallace88877
 
Write a page answering the questions below.Sometimes projects .docx
Write a page answering the questions below.Sometimes projects .docxWrite a page answering the questions below.Sometimes projects .docx
Write a page answering the questions below.Sometimes projects .docxedgar6wallace88877
 
Write a one-paragraph summary of one of the reading assignments from.docx
Write a one-paragraph summary of one of the reading assignments from.docxWrite a one-paragraph summary of one of the reading assignments from.docx
Write a one-paragraph summary of one of the reading assignments from.docxedgar6wallace88877
 
Write a one-paragraph summary of this article.Riordan, B. C..docx
Write a one-paragraph summary of this article.Riordan, B. C..docxWrite a one-paragraph summary of this article.Riordan, B. C..docx
Write a one-paragraph summary of this article.Riordan, B. C..docxedgar6wallace88877
 
Write a one-paragraph response to the following topic. Use the MLA f.docx
Write a one-paragraph response to the following topic. Use the MLA f.docxWrite a one-paragraph response to the following topic. Use the MLA f.docx
Write a one-paragraph response to the following topic. Use the MLA f.docxedgar6wallace88877
 
Write a one-page rhetorical analysis in which you analyze the argume.docx
Write a one-page rhetorical analysis in which you analyze the argume.docxWrite a one-page rhetorical analysis in which you analyze the argume.docx
Write a one-page rhetorical analysis in which you analyze the argume.docxedgar6wallace88877
 
Write a one pageliterature review of your figure( FIGURE A.docx
Write a one pageliterature review of your figure( FIGURE A.docxWrite a one pageliterature review of your figure( FIGURE A.docx
Write a one pageliterature review of your figure( FIGURE A.docxedgar6wallace88877
 
Write a one page-paper documenting the problemneed you wish to .docx
Write a one page-paper documenting the problemneed you wish to .docxWrite a one page-paper documenting the problemneed you wish to .docx
Write a one page-paper documenting the problemneed you wish to .docxedgar6wallace88877
 
Write a one page report on Chapter 1 and 2 with the same style of mo.docx
Write a one page report on Chapter 1 and 2 with the same style of mo.docxWrite a one page report on Chapter 1 and 2 with the same style of mo.docx
Write a one page report on Chapter 1 and 2 with the same style of mo.docxedgar6wallace88877
 
Write a one page reflection about the following1) Identify .docx
Write a one page reflection about the following1) Identify .docxWrite a one page reflection about the following1) Identify .docx
Write a one page reflection about the following1) Identify .docxedgar6wallace88877
 
Write a one page paper on the question belowSome of the current.docx
Write a one page paper on the question belowSome of the current.docxWrite a one page paper on the question belowSome of the current.docx
Write a one page paper on the question belowSome of the current.docxedgar6wallace88877
 
Write a one page paper (double spaced) describing and discussing the.docx
Write a one page paper (double spaced) describing and discussing the.docxWrite a one page paper (double spaced) describing and discussing the.docx
Write a one page paper (double spaced) describing and discussing the.docxedgar6wallace88877
 
write a one page about this topic and provide a reference.Will.docx
write a one page about this topic and provide a reference.Will.docxwrite a one page about this topic and provide a reference.Will.docx
write a one page about this topic and provide a reference.Will.docxedgar6wallace88877
 
Write a one or more paragraph on the following question below.docx
Write a one or more paragraph on the following question below.docxWrite a one or more paragraph on the following question below.docx
Write a one or more paragraph on the following question below.docxedgar6wallace88877
 
Write a one or more page paper on the following belowWhy are .docx
Write a one or more page paper on the following belowWhy are .docxWrite a one or more page paper on the following belowWhy are .docx
Write a one or more page paper on the following belowWhy are .docxedgar6wallace88877
 
Write a one page dialogue in which two characters are arguing but .docx
Write a one page dialogue in which two characters are arguing but .docxWrite a one page dialogue in which two characters are arguing but .docx
Write a one page dialogue in which two characters are arguing but .docxedgar6wallace88877
 

More from edgar6wallace88877 (20)

Write a page to a page and half for each topic and read each topic a.docx
Write a page to a page and half for each topic and read each topic a.docxWrite a page to a page and half for each topic and read each topic a.docx
Write a page to a page and half for each topic and read each topic a.docx
 
Write a page discussing why you believe PMI is focusing BA as the fi.docx
Write a page discussing why you believe PMI is focusing BA as the fi.docxWrite a page discussing why you believe PMI is focusing BA as the fi.docx
Write a page discussing why you believe PMI is focusing BA as the fi.docx
 
Write a page of personal reflection of your present leadership compe.docx
Write a page of personal reflection of your present leadership compe.docxWrite a page of personal reflection of your present leadership compe.docx
Write a page of personal reflection of your present leadership compe.docx
 
Write a page of compare and contrast for the Big Five Personalit.docx
Write a page of compare and contrast for the Big Five Personalit.docxWrite a page of compare and contrast for the Big Five Personalit.docx
Write a page of compare and contrast for the Big Five Personalit.docx
 
Write a page of research and discuss an innovation that includes mul.docx
Write a page of research and discuss an innovation that includes mul.docxWrite a page of research and discuss an innovation that includes mul.docx
Write a page of research and discuss an innovation that includes mul.docx
 
Write a page answering the questions below.Sometimes projects .docx
Write a page answering the questions below.Sometimes projects .docxWrite a page answering the questions below.Sometimes projects .docx
Write a page answering the questions below.Sometimes projects .docx
 
Write a one-paragraph summary of one of the reading assignments from.docx
Write a one-paragraph summary of one of the reading assignments from.docxWrite a one-paragraph summary of one of the reading assignments from.docx
Write a one-paragraph summary of one of the reading assignments from.docx
 
Write a one-paragraph summary of this article.Riordan, B. C..docx
Write a one-paragraph summary of this article.Riordan, B. C..docxWrite a one-paragraph summary of this article.Riordan, B. C..docx
Write a one-paragraph summary of this article.Riordan, B. C..docx
 
Write a one-paragraph response to the following topic. Use the MLA f.docx
Write a one-paragraph response to the following topic. Use the MLA f.docxWrite a one-paragraph response to the following topic. Use the MLA f.docx
Write a one-paragraph response to the following topic. Use the MLA f.docx
 
Write a one-page rhetorical analysis in which you analyze the argume.docx
Write a one-page rhetorical analysis in which you analyze the argume.docxWrite a one-page rhetorical analysis in which you analyze the argume.docx
Write a one-page rhetorical analysis in which you analyze the argume.docx
 
Write a one pageliterature review of your figure( FIGURE A.docx
Write a one pageliterature review of your figure( FIGURE A.docxWrite a one pageliterature review of your figure( FIGURE A.docx
Write a one pageliterature review of your figure( FIGURE A.docx
 
Write a one page-paper documenting the problemneed you wish to .docx
Write a one page-paper documenting the problemneed you wish to .docxWrite a one page-paper documenting the problemneed you wish to .docx
Write a one page-paper documenting the problemneed you wish to .docx
 
Write a one page report on Chapter 1 and 2 with the same style of mo.docx
Write a one page report on Chapter 1 and 2 with the same style of mo.docxWrite a one page report on Chapter 1 and 2 with the same style of mo.docx
Write a one page report on Chapter 1 and 2 with the same style of mo.docx
 
Write a one page reflection about the following1) Identify .docx
Write a one page reflection about the following1) Identify .docxWrite a one page reflection about the following1) Identify .docx
Write a one page reflection about the following1) Identify .docx
 
Write a one page paper on the question belowSome of the current.docx
Write a one page paper on the question belowSome of the current.docxWrite a one page paper on the question belowSome of the current.docx
Write a one page paper on the question belowSome of the current.docx
 
Write a one page paper (double spaced) describing and discussing the.docx
Write a one page paper (double spaced) describing and discussing the.docxWrite a one page paper (double spaced) describing and discussing the.docx
Write a one page paper (double spaced) describing and discussing the.docx
 
write a one page about this topic and provide a reference.Will.docx
write a one page about this topic and provide a reference.Will.docxwrite a one page about this topic and provide a reference.Will.docx
write a one page about this topic and provide a reference.Will.docx
 
Write a one or more paragraph on the following question below.docx
Write a one or more paragraph on the following question below.docxWrite a one or more paragraph on the following question below.docx
Write a one or more paragraph on the following question below.docx
 
Write a one or more page paper on the following belowWhy are .docx
Write a one or more page paper on the following belowWhy are .docxWrite a one or more page paper on the following belowWhy are .docx
Write a one or more page paper on the following belowWhy are .docx
 
Write a one page dialogue in which two characters are arguing but .docx
Write a one page dialogue in which two characters are arguing but .docxWrite a one page dialogue in which two characters are arguing but .docx
Write a one page dialogue in which two characters are arguing but .docx
 

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx

  • 1. Skip To ContentOpen Quick LinksQuick Links Page Landmarks Content Outline Keyboard Shortcuts Logout Global Menu Jerry ShenActivity Updates43 Home Help Top Frame TabsHome Tab 1 of 1 (active tab) Current Location Programming for Data Science (Fall 2019) Module 2: Control Flow Project 2: Playing the Lotto Menu Management Options Course Menu: Programming for Data Science (Fall 2019) AnnouncementsWELCOMEModule 0: Start here! (Setup)RESOURCESSyllabusScheduleCourse FAQOffice and Consulting HoursMy GradesEmail ProfessorCOURSE MODULESModule 1: Variables and Data TypesModule 2:
  • 2. Control FlowModule 3: Text Analysis with Python objects My Groups CSCI140-01 Project 2: Playing the Lotto Content Project 2: Playing the Lotto Attached Files: Project_2_EC.py (451 B) Project_2_Main.py
  • 3. (114 B) Project2_vN14.pdf (205.725 KB) Project_2.py (1.226 KB) For this project, you will create and correct a set of functions that simulate playing the lottery, and you will write a main
  • 4. program that makes use of your functions. The .pdf below contains the specifications for the project. Skeleton python files are provided in Project_2.py and Project_2_EC.py - you should not delete any of the def lines from this file - if you do not fill in a function definition, leave the word pass indented under that definition, and Project_2_Main.py - this file is for your main program. Edited 9/23: You should not need to change order of lines for debugging. You may need to change indentation. Video Showing how the Project Files work This video shows you how the two project files should look (what they should and should not contain), how they work together, and how to run the project from the command line. Watch it - this is the #1 question about project 2, and the cause of a lot of problems. CSCI 140 Programming for Data Science Project 2 Checkpoint Deadline 1700, Sunday September 29 Due 1700, Friday, October 4 In our second project, you are tasked with developing functions to simulate playing the lottery. You will create two functions, correct a third function, and write a short main program. N.B.: You must use the structures we have learned in this class
  • 5. to complete this assignment. Implementations using structures we have not covered may receive no credit. For example, you may not use string and list methods since we have not formally talked about methods. We have provided two skeleton files: Project_2.py for your function code, and Project_2_Main.py for your main program. Function One: Generating Lottery Numbers Many states in the United States have a state lottery. Players pick typically between 4 and 6 numbers within a certain range, and then win monetary prizes based on how many of their numbers match numbers randomly drawn in the lottery. For example, a lottery could use the numbers from 1 to 50, and players must pick 5 numbers. Each number can only be chosen once, that is, each number is unique. For example, a player could choose: 39 7 12 14 42 Write a function called generate_numbers that randomly selects lottery numbers. This function has two optional arguments: limit – the maximum number used in the lottery, i.e. the highest number a player can choose, and num – the number of lottery numbers to select. The default value of limit is 34 and the default value of num is 5. These defaults correspond to one of the Virginia lottery games. Your function will return a list of randomly selected lottery numbers. Here are some examples – numbers are selected randomly so these show only 1 possible outcome.
  • 6. • generate_numbers with default values for limit and num Example return value: [20, 10, 34, 12, 31] • generate_numbers with default value for limit, num = 8: Example return value: [16, 14, 12, 28, 26, 15, 30, 32] • generate_numbers with limit = 55, num = 4: Example return value [11, 26, 18, 48] You can assume that the input for limit will always be a positive integer greater than or equal to num. The input for num will be a positive integer. Key points: • Optional argument limit gives the highest possible lottery number that can be chosen; inputs tested will always be correct type • Each lottery number can only be chosen ONCE, they cannot be repeated • Optional argument num is an integer; default value: 5; inputs tested always correct type • Return a list of num integers randomly selected with no duplicates between 1 and limit • You can do this with a loop, but it is actually easier to use one of the functions from the random module without a loop; HINT: how can we generate the numbers from 1 to limit?
  • 7. Function Two:Win or Lose To win the lottery, you must match your numbers to the winning numbers. The prize you win corresponds to how many of your numbers match the winning numbers. The more numbers you match, the larger the prize. For example, if your numbers are: 15 32 42 10 18 and the winning numbers are: 32 42 15 18 9, you have matched four out of five numbers and would win the second largest prize. Typically, you don’t win a prize for matching very few numbers, e.g. in the Virginia lottery, you won’t get anything if you match only one or two numbers to the winning numbers. You have been code for a function called lotto_prize which needs to be corrected. You may not add or delete lines. You may change indentation, but should not change the order of the lines. The function lotto_prize takes three required arguments: attempt – a list containing a player’s lottery numbers, winning – the winning lottery numbers, and prizes – a list containing the prize amounts from largest to smallest. The function will return a numeric value that is the prize won, either an item from prizes or 0. Whether and how much the player wins, and which output is returned, is based on how many numbers match. Matching all numbers gets the largest prize, all but one the second largest, and so forth. If they match too few numbers, they win nothing. The examples below are for demonstration only, your function
  • 8. should PRINT NOTHING. • All of the player’s numbers match: player wins the largest prize attempt is [28, 49, 2, 55, 33, 44], winning is [2, 28, 49, 33, 44, 55] prizes is [1000000, 400, 20, 5] Function returns 1000000 • All but one of the player’s numbers match: player wins the second largest prize attempt is [28, 49, 2, 33, 44], winning is [2, 28, 9, 33, 44] prizes is [1000, 40, 1] Function returns 40 • Player matches too few numbers to win: player wins nothing attempt is [28, 49, 2, 33, 44, 19, 4], winning is [50, 49, 27, 31, 2, 7, 11] prizes is [1250000, 10000, 1500, 50] Function returns 0 The inputs for attempt and winning will always be of the same length, but that length can be any positive integer. The input for prizes is a list sorted from largest to smallest with a length less than or equal to the length of attempt and winning. No other inputs will be tested.
  • 9. Key points: • The text of the code you are given is ALMOST correct and requires only minor changes. These changes mostly have to do with logical errors and things like indexing. You need to preserve as much of the original code as possible. You may need to change indentation for some lines. You do not need to and are not permitted to add any lines or delete any lines. You must understand in detail what each line is meant to do and how to solve the problem on paper. READ THE LAST KEY POINT AND COMMENTS IN CODE VERY CAREFULLY. • All of the required arguments are lists: attempt and winning are the same length and are unsorted; prizes has a length less than or equal to attempt and winning and is sorted largest to smallest - this means the user inputs it sorted, your function does not sort it. • Function returns an integer that is either the prize amount of 0; prize won depends on how many numbers match • The function is written to operate in this general manner: check if each item from attempt is part of winning, count how many items match. Use count of matches to determine if the player wins a prize, and if so, which prize from prizes they win. Return prize amount.
  • 10. Function Three: Can you really win? You decide to play a set of lottery numbers until you win the jackpot, that is until your numbers match all of the winning numbers. How many times would you have to play before this happens? Write a function called simulate_lotto that takes two required arguments: your_numbers –a list containing your lottery numbers and max_pick – the highest number available to pick in the lottery – that is, the highest value that could appear in your numbers or the winning numbers. simulate_lotto also takes an optional argument max_plays which has a default value of 10,000. The purpose of max_plays is to prevent your code from running forever. In a lottery with 6 numbers chosen from 1 to 50, there are 11,441,304,000 different sets of winning numbers, which doesn’t even account for the same numbers being chosen twice. Your program could run for a LONG time before the player’s numbers win. max_plays limits how many times winning numbers will be chosen. Even if the player hasn’t won, the function will stop after max_plays lottery picks. simulate_lotto should return an integer which is the number of plays with your numbers it took to win the jackpot –this means all of the numbers match- OR max_plays, whichever is smaller.
  • 11. Here are some examples to demonstrate how the function works. This is NOT printed OUTPUT. • Example #1: your_numbers is [45, 23, 12, 22], max_pick is 50, max_plays is 10,000 Play 1: winning numbers are [12, 49, 2, 45] Play 2: winning numbers are [7, 8, 41, 13] Play 3: winning numbers are [12, 23, 45, 22] Function returns 3. Notice that the order of the numbers doesn’t matter, just that the winning numbers match your_numbers. Once they match, the function terminates. • Example #2: Example #1: your_numbers is [5, 2, 1], max_pick is 10, max_plays is 4 Play 1: winning numbers are [1, 4, 2] Play 2: winning numbers are [7, 8, 4] Play 3: winning numbers are [1, 3, 4] Play 4: winning numbers are [1,7,9] Function returns 4 – the value of max_plays. Even though the player didn’t win (winning numbers were never [5,2,1]), the function terminates because the max number of plays was reached. Key points: • Input for your_numbers will always be a list of integers. The
  • 12. length of this list can vary. • Input for max_pick will always be a positive integer greater than or equal to the length of your_numbers. Input for max_plays will always be a positive integer. • The winning numbers are generated by your function – they are NOT input by the user. You MUST use your generate_numbers function. Figure out how many numbers to generate from the user input your_numbers – HINT: look at the length of the input. • You MUST use your lotto_prize function to determine whether or not the user won the jackpot. Winning the jackpot means that all numbers match. Think about how to specify the prizes argument to make this work. • You need to use a loop in this function. We have not discussed break or continue. Do not use them. Stop your loop either using logic or by exploiting how return works. MainProgram: Playing the Lottery You will write a brief main program that makes use of your functions. This must be saved and submitted in Project_2_Main.py. The main program must make use of your functions. You will be graded specifically on coding style for this part of the project – that means writing efficient code that is not repetitive, and does not specifying default
  • 13. arguments when possible. The main program should do the following: • Generate five lottery numbers to play where the numbers range from 1 to 65. Use one of your functions to do this. • Generate a second set of five lottery numbers to be the winning numbers. These also range from 1 to 65. Use one of your functions to do this. • Using the two sets of numbers you generated above, calculate the prize the user wins. The available prizes are: $100000 for all matching numbers, $500 for 4 matching numbers, and $50 for 3 matching numbers. Use one of your functions to do this. Print out the prize. • Generate six lottery numbers to play where the numbers range from 1 to 20. Use one of your functions to do this. Simulate playing these numbers until you win the jackpot or you reach 1000 plays. Use one of your functions to do this. Print out how many plays it took. Extra Credit Worth up to 3% total. Fill in under def lines in Project_2_EC.py 1) Many lotteries have a final number drawn that is a “super pick” which has its own prizes and/or can double/triple/etc. the prize won using the other numbers.
  • 14. For example, you might pick 5 regular lottery numbers between 1 and 50 and then pick a final number between 1 and 20: 45 2 5 14 29 Super pick: 11 Fill in the function definition for generate_numbers_super to add support for a single extra “super” pick. Keep in mind that the range of numbers for the super pick may be different from the range for the regular lottery numbers, and this function has an additional parameter super_limit which is the upper bound for the “super pick” (the highest number the super pick can be). The return value is a list where the LAST item in the list is the super pick. 2) Fill in the function definition for lotto_prize_super to add support for a super pick as described in 1. This function has two additional arguments, s_num - a Boolean indicating if the lottery numbers provided indicate a super pick as the last number, and multiplier – this indicates what to multiply the prize by if the user wins. Here’s an example of calling the function: lotto_prize_super([43,57,23,45,1], [40, 45, 32, 23,1],[100000,100, 10], True, 2) In this case, since s_num is True, 1 is separated from the other numbers. The user matched two regular numbers (45 and 23), so they would win $10. Since they also matched the super pick and multiplier is 2, this is doubled and the value returned is 20. 3) The corrected implementation of lotto_prize finds matches a
  • 15. particular way. It might be more efficient to determine matches by sorting the lists first and/or implementing a search algorithm. Or you could implement this function more concisely by using objects other than lists. Fill in the function definition for lotto_prize_ search with any enhancements you can come up with. SUBMISSION EXPECTATIONS Project_2.py Your implementations of functions one, two (corrected), and three. Do not change the names of parameters in the function def lines or the names of the functions – if you do so, it will affect your grade. No additional code including input lines, print lines, and function calls should be submitted. Make sure that you have the correct import lines. If your code requires manual intervention to run such as adding/editing import lines, correcting indentation, removing print and input lines, etc., and/or correcting syntax errors, you can expect up to a 5 point deduction. Project_2_Main.py Your implementation of the main program specified in Part 4. Your functions must not be repeated in this file. They must be imported using the correct import line. Make sure that you have the correct import lines. If your code requires manual intervention, you can expect up to a 5 point deduction. Project_2_EC.py Your implementations of any of the extra credit functions. They must be
  • 16. submitted in this file. No additional code including input lines, print lines, and function calls should be submitted. Make sure that you have the correct import lines. Project_2.pdf A PDF document containing your reflections on the project. You must also cite any sources you use. Please be aware that you can consult sources, but all code written must be your own. Programs copied in part or wholesale from the web or other sources will receive 0 points and will result in reporting of an Honor Code violation. N.B.: You must use the structures we have learned in this class to complete this assignment. Implementations using structures we have not covered may receive no credit. This means NO LIST AND STRING METHODS (yes, that includes append – do not use it). POINT VALUES AND GRADING RUBRIC Function 1: 27.5 points Function 2: 27.5 points Function 3: 32.5 points Main Program: 10 points Write-up: 2.5 points SUGGESTIONS FOR COMPLETION TIMELINE This project integrates concepts from many lectures. The most
  • 17. important step will be to understand what each function does and figure out the necessary steps. Make sure to work things out on paper first. This schedule sets you up to submit all functions for the checkpoint. -Read through instructions and make sure you understand what each function does and how it works by 9/23 – bring questions to office hours and lab; start writing steps on paper ASAP -Complete debugging Function 2 after lecture 9/23-9/24 -Complete Function 1 and first three steps of main program after lecture 9/25-9/26 (you could complete this after lecture 9/23-9/24 but it will be easier with material from 9/25-9/26) -Complete Function 3 and last step of main program after lecture 9/25-9/26 (you could complete this after 9/23-9/24 but it will be easier with material from 9/25-9/26) Traceback (most recent call last): File "Prj2_test.py", line 3, in <module> from Project_2 import * ModuleNotFoundError: No module named ’Project_2’ $ 0
  • 18. 1000 $ 0 100 generate_numbers_super_defaults: Generated [8, 12, 1, 27, 5, 14] Checking min, max, length, and frequency generate_numbers_super_equal: Sample larger than population or is negative Testing failed. lotto_prize_search_jackpot_same: attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes: [10000, 1000, 100, 10, 1] Return value was 10000 lotto_prize_search_jackpot_shuffle: attempt: [5, 15, 2, 24, 56] winning: [24, 15, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 10000 lotto_prize_search_jackpot_shuffle_2: attempt: [5, 15, 2, 24, 56] winning: [24, 35, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 1000 lotto_prize_search_lose: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 0 lotto_prize_search_prize_odd: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes: [10000, 1000, 100] Return value was 0
  • 19. lotto_prize_search_prize_odd_2: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 35, 13, 2, 11, 19, 42] prizes: [10000, 1000, 100] Return value was 0 lotto_prize_prize_search_odd_3: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 15, 13, 2, 11, 19, 42] prizes: [10000, 1000, 100] Return value was 0 lotto_prize_prize_search_odd_4: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 13, 2, 11, 19, 56] prizes: [10000, 1000, 100] Return value was 100 lotto_prize_search_prize_odd_5: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11, 19, 56] prizes: [10000, 1000, 100] Return value was 100 lotto_prize_search_prize_one: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11, 29, 56] prizes: [10000] Return value was 0 lotto_prize_search_prize_one_2: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [2, 5, 19, 24, 77, 15, 56] prizes: [10000] Return value was 10000 lotto_prize_search_shuffle_3: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 100
  • 20. lotto_prize_search_shuffle_4: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 10 lotto_prize_search_shuffle_5: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 1 lotto_prize_super_jackpot_same: attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes: [10000, 1000, 100, 10, 1] local variable ’prize’ referenced before assignment Testing failed. lotto_prize_super_jackpot_same_2: attempt: [5, 15, 2, 24, 56, 2] winning: [5, 15, 2, 24, 56, 2] prizes: [10000, 1000, 100, 10, 1] Mult:4 Return value was 40000 lotto_prize_super_jackpot_shuffle: attempt: [5, 15, 2, 24, 56, 6] winning: [24, 15, 56, 2, 5, 6] prizes: [10000, 1000, 100, 10, 1] Mult:5 Return value was 50000 lotto_prize_super_lose: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes: [10000, 1000, 100, 10] Return value was 0 lotto_prize_super_shuffle_1: attempt: [5, 15, 2, 24, 56, 32] winning: [44, 35, 56, 2, 11, 32]
  • 21. prizes: [10000, 1000, 100, 10, 1] Mult:4 Return value was 40 Traceback (most recent call last): File "Prj2_test.py", line 3, in <module> from Project_2 import * ModuleNotFoundError: No module named ’Project_2’ $ 0 1000 $ 0 100 generate_numbers_super_defaults: Generated [8, 12, 1, 27, 5, 14] Checking min, max, length, and frequency generate_numbers_super_equal: Sample larger than population or is negative Testing failed. lotto_prize_search_jackpot_same: attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes: [10000, 1000, 100, 10, 1] Return value was 10000 lotto_prize_search_jackpot_shuffle: attempt: [5, 15, 2, 24, 56] winning: [24, 15, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 10000 lotto_prize_search_jackpot_shuffle_2: attempt: [5, 15, 2, 24, 56] winning: [24, 35, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 1000
  • 22. lotto_prize_search_lose: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 0 lotto_prize_search_prize_odd: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes: [10000, 1000, 100] Return value was 0 lotto_prize_search_prize_odd_2: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 35, 13, 2, 11, 19, 42] prizes: [10000, 1000, 100] Return value was 0 lotto_prize_prize_search_odd_3: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 15, 13, 2, 11, 19, 42] prizes: [10000, 1000, 100] Return value was 0 lotto_prize_prize_search_odd_4: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 13, 2, 11, 19, 56] prizes: [10000, 1000, 100] Return value was 100 lotto_prize_search_prize_odd_5: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11, 19, 56] prizes: [10000, 1000, 100] Return value was 100 lotto_prize_search_prize_one: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11, 29, 56] prizes: [10000] Return value was 0
  • 23. lotto_prize_search_prize_one_2: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [2, 5, 19, 24, 77, 15, 56] prizes: [10000] Return value was 10000 lotto_prize_search_shuffle_3: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 100 lotto_prize_search_shuffle_4: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 10 lotto_prize_search_shuffle_5: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 1 lotto_prize_super_jackpot_same: attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes: [10000, 1000, 100, 10, 1] local variable ’prize’ referenced before assignment Testing failed. lotto_prize_super_jackpot_same_2: attempt: [5, 15, 2, 24, 56, 2] winning: [5, 15, 2, 24, 56, 2] prizes: [10000, 1000, 100, 10, 1] Mult:4 Return value was 40000 lotto_prize_super_jackpot_shuffle: attempt: [5, 15, 2, 24, 56, 6] winning: [24, 15, 56, 2, 5, 6]
  • 24. prizes: [10000, 1000, 100, 10, 1] Mult:5 Return value was 50000 lotto_prize_super_lose: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes: [10000, 1000, 100, 10] Return value was 0 lotto_prize_super_shuffle_1: attempt: [5, 15, 2, 24, 56, 32] winning: [44, 35, 56, 2, 11, 32] prizes: [10000, 1000, 100, 10, 1] Mult:4 Return value was 40 from Project_2_EC import * from Project_21 import * from random import randint, choice, choices, sample, shuffle import sys #Your main program goes here #Main Program #1 attempt = generate_numbers(65, 5) #2 winning = generate_numbers(65, 5) #3 prizes = [100000, 500, 50] prize = lotto_prize(attempt,winning, prizes) print("$", prize) #4 max_pick = 20 max_play = 1000 winning_number = generate_numbers(20, 6) plays = simulate_lotto(winning_number, max_pick, max_play) print(plays) #Extra Credits
  • 25. #1 attempt = generate_numbers_super(65,5,20) winning = generate_numbers_super(65,5,20) prizes = [1000,100,10] #2 prize = lotto_prize_super(attempt, winning, prizes, True, 5) print("$", prize) #3 print(lotto_prize_search([1,2,3,4,5],[1,2,3,4,6],[1000,100,10])) from random import randint, choice, choices, sample, shuffle from Project_21 import * def generate_numbers_super(limit = 34, num = 5, super_limit = 20): return sample(range(1,limit),num) + sample (range(super_limit),1) def lotto_prize_super(attempt, winning, prizes, super_ = False, multiplier = 1): if super_: prize = lotto_prize(attempt, winning, prizes) super_attempt = attempt[-1] super_winning = winning[-1] if super_attempt == super_winning: prize *= multiplier return prize else: return lotto_prize(attempt, winning, prize) def lotto_prize_search(attempt, winning, prizes): matches = len(set(attempt).intersection(winning)) count = len(winning) for prize in prizes:
  • 26. if(matches == count): return prize count = count - 1 return 0 #Absolutely no additional code in this file #No function calls #No input lines #There should be no unindented code other than the import line and the def lines Hi Everyone, Checkpoint results are now posted in Blackboard. Great work! A few notes: -Double check file names, import lines, and indentation. -Check for infinite loops. If your file got stopped b/c of the infinite loop in the debugging I commented or exited out of it so you could still get results. But there were other student-created infinite loops. -No tests for Main Program. Some tests for EC. -Unless you see the printed messaged "Testing failed" under a test, your code passed the test -All of the lotto prize tests show the inputs and your return value - you can figure out the expected return value from the inputs -Double check you followed instructions ESPECIALLY for the debugging -Here's an example of a run with no errors with a few notes on some tests: generate_numbers_default_limit: #default for limit Generated [12, 34, 32, 24, 8, 1, 18, 17] Checking min, max, length, and frequency . generate_numbers_default_num: #default for num Generated [7, 125, 143, 135, 139]
  • 27. Checking min, max, length, and frequency . generate_numbers_defaults: #default for both Generated [5, 3, 23, 24, 7] Checking min, max, length, and frequency . generate_numbers_equal: #num and limit equal, forces every number up to limit to be used Generated [6, 4, 3, 8, 9, 2, 5, 10, 1, 7] Checking min, max, length, and frequency . generate_numbers_rand_test: #checks randomness, selections are only 1,2,3 - checks frequency of each possible combination and for incorrect limits Out of 60000 items, 9808 were [1,2,3]. Expected ~10000. 9975 were [2,1,3]. Expected ~10000. 9954 were [3,1,2]. Expected ~10000. 10097 were [1,3,2]. Expected ~10000. 10074 were [2,3,1]. Expected ~10000. 10092 were [3,2,1]. Expected ~10000. 0 did not match the specified options. THIS MUST BE 0. . lotto_prize_prize_even: attempt: [5, 15, 2, 24] winning: [2, 25, 19, 24] prizes: [10000, 562] Return value was 0 . lotto_prize_prize_even_2: attempt: [5, 15, 200, 24] winning: [200, 15, 19, 24] prizes: [10000, 562] Return value was 562 . lotto_prize_jackpot_same: attempt: [5, 15, 2, 24, 56] winning: [5, 15, 2, 24, 56] prizes:
  • 28. [10000, 1000, 100, 10, 1] Return value was 10000 . lotto_prize_jackpot_shuffle: #numbers out of order - all tests that say shuffle attempt: [5, 15, 2, 24, 56] winning: [24, 15, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 10000 . lotto_prize_jackpot_shuffle_2: attempt: [5, 15, 2, 24, 56] winning: [24, 35, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 1000 . lotto_prize_lose: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 20, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 0 . lotto_prize_prize_odd: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes: [10000, 1000, 100] Return value was 0 . lotto_prize_prize_odd_2: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 35, 13, 2, 11, 19, 42] prizes: [10000, 1000, 100] Return value was 0 . lotto_prize_prize_odd_3: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [44, 15, 13, 2, 11, 19, 42] prizes: [10000, 1000, 100] Return value was 0 . lotto_prize_prize_odd_4: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 13, 2, 11,
  • 29. 19, 56] prizes: [10000, 1000, 100] Return value was 100 . lotto_prize_prize_odd_5: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11, 19, 56] prizes: [10000, 1000, 100] Return value was 100 . lotto_prize_prize_one: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [24, 15, 23, 2, 11, 29, 56] prizes: [10000] Return value was 0 . lotto_prize_prize_one_2: attempt: [5, 15, 2, 24, 56, 77, 19] winning: [2, 5, 19, 24, 77, 15, 56] prizes: [10000] Return value was 10000 . lotto_prize_shuffle_3: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 5] prizes: [10000, 1000, 100, 10, 1] Return value was 100 . lotto_prize_shuffle_4: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 56, 2, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 10 . lotto_prize_shuffle_5: attempt: [5, 15, 2, 24, 56] winning: [44, 35, 13, 2, 11] prizes: [10000, 1000, 100, 10, 1] Return value was 1 . simulate_lotto_max_plays: #forces max plays Return value was 10 .
  • 30. simulate_lotto_max_plays_large: #forces max plays Return value was 1500 . simulate_lotto_mult_MC: #based on Monte Carlo simulation - runs the function many times and calcs average, if you failed this, there is a small chance your code is OK - depends on probability On average, it took 9.8915 plays before you ran out of money. . simulate_lotto_single: #length of attempt and limit are the same, so must win on first play, inputs are [1,2,3,4,5] and 5 Return value was 1 . Project1/convert_age.py Age_on_Earth=float(input('Enter your age on Earth: ')) days=[365.25,87.97,687,4331.87,10760.27,60189.55] planet=int(input('Select a planet:1 for Merccury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune: ')) Age_on_planet=(365.25/days[planet])*Age_on_Earth Types_of_planet=['Earth','Merccury','Mars','Jupiter','Saturn','Ne ptune'] print('Your age on ', Types_of_planet[planet] , 'is ',Age_on_planet,' years') Project1/convert_age_2.py Home_planet=int(input('Select your home planet: 1 for
  • 31. Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune: ')) Age_on_home_planet=float(input('Enter your age on your home planet: ')) Destination=int(input('Select your destinaion planet: 1 for Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune: ')) planet=[365,87.97,687,4331.87,10760.27,60189.55] planets=['Earth','Mercury','Mars','Jupiter','Saturn','Neptune'] Age=(planet[Home_planet]/planet[Destination])*Age_on_home _planet print('Your age on',str(planets[Destination]),'is',Age,'years') Project1/Earth_to_Mars.py Earth_years = float(input('Enter the number of Earth years: ')) Earth_TO_Mars = 365/687 value = Earth_years*Earth_TO_Mars print('This is',value,'years on Mars') Project1/overdue_tax.py PENALTY_RATE = 0.06 #Do not change EARTH_YEAR = 365 #Do not change tax = input('Enter amount of tax owed: ') #Do not change, will
  • 32. be in format $100 rate = input('Enter interest rate: ') #Do not change, will be in format 4% late = int(input('Enter number of days overdue tax is: ')) #Do not change, will be an integer penalty = PENALTY_RATE * float(tax[1:]) interest = (float(rate[:- 1])/100)*(late/EARTH_YEAR)*float(tax[1:]) total = float(tax[1:])+penalty+interest print('Your total payment is' , total) Project1/Project1_v9.pdf CSCI 140 Programming for Data Science Project 1 Due Friday, 13 September 2019 at 1700 Checkpoint Deadline: Tuesday, 10 September 2019 at 1700 In our first project, your task is to create 2 short programs, complete one program, and correct a fourth program. There are four programs to submit, each performing one task. They can all be completed using only the material introduced in the first couple of days of class. You will submit these are separate files with the extension .py, but you can write and test your programs in a notebook if you prefer. In the problem solving session, you will practice creating, saving, and running .py files. We suggest that for each program you write an algorithm as a simple numbered list of steps and then create a Python implementation. Your submission will consist of a PDF document containing
  • 33. your reflections on the project, and four Python programs saved in .py files named as specified. Program One: Convert Earth Years to Mars Years Elon Musk plans to retire on Mars. One year on Mars is 687 Earth days. One year on Earth is 365.25 Earth days. So, for example, if he stays on Mars for 16 Mars years, that’s about 30 Earth years. Write a program that solicits a single input from the user: a number of Earth years. Your program should convert the Earth years to Mars years using the information given above. You must format the program's interaction as illustrated in the following examples. Do not vary the phrasing, capitalization, spacing, etc. You must also write a properly styled, easily readable and understandable program. Enter number of Earth years: 10 This is 5.316593886462882 years on Mars. Enter number of Earth years: 0.5 This is 0.26582969432314413 years on Mars. Your program must be called Earth_to_Mars.py and should work for any non-negative numeric input provided by the user. Other inputs will not be tested.
  • 34. Program Two: Calculate Retirement Contributions Speaking of retirement, you need to start saving early if you’re going to make it to Mars. Many employers offer workplace retirement accounts. Money is deducted (taken out) of an individual’s salary and is put into a separate account that is not accessible (usually) until retirement. Individuals get to select the amount they want to save per paycheck. Suppose that an individual wants to try to have the same amount deducted from every paycheck to hit a target for retirement savings. Many workplaces only allow you to deduct money for retirement as full dollar amounts, that is $50 as opposed to $50.50. This means that the last payment for the year will have to include a few extra dollars to hit their savings target. Here are some examples of the calculations – this is not code and is not what your program’s output should look like. Example 1: Target is $10000 and individual is paid 12 times per year They save $833 dollars from the first 11 paychecks, which gives a total of $9163, so they need to save an extra $4 in the last paycheck, for a total of $837, to reach the target of $10000.
  • 35. (You might be wondering, why not just save $834 each paycheck? The goal is not to overshoot the target, because if you overshoot the IRS limit, you’ll find yourself in a lot of trouble.) Example 2: Target is $3600 and individual is paid 24 times per year In this case, they can save $150 from each paycheck and meet the target exactly. There is no extra money to deduct from the last paycheck. Write a program that calculates the amount of each equal deduction per paycheck, and how many extra dollars should be added to the last payment to hit the target. Your program will solicit two inputs from the user: a target amount in whole dollars (no cents) and the number of times an individual is paid per year. Save this program in a file called retirement.py. Program execution should look like what is shown below. Pay special attention to formatting, spacing, and capitalization of the input and output lines. Enter total amount to save: 10000 Enter number of paychecks per year: 12 You must make 11 deductions of $833 and one final deduction of $837 to save $10000 Enter total amount to save: 3600 Enter number of paychecks per year: 24 You must make 23 deductions of $150 and one final deduction of $150 to save $3600
  • 36. Note that the output is the same whether or not the final payment is a different value. You can assume that the inputs for the target and number of paychecks will be positive integers, and your program should work for any such inputs. Program Three: Fountain of Youth In Program 1, you converted from years on Earth to years on Mars. We now want to consider other planets, and figure out what your age would be on other planets, using their definition of a year. For our purposes, a ‘year’ is the time it takes for a particular planet to orbit the sun (a revolution). The table below shows how long a year lasts on other planets in terms of Earth days: Mercury 87.97 Earth days Mars 687 Earth days Jupiter 4331.87 Earth days Saturn 10760.27 Earth days Neptune 60189.55 Earth days
  • 37. Write a program to convert your age in Earth years to your age in years on any of these 5 planets. You have been given three lines of code that you cannot change in any way which must be used for your program in the file convert_age.py. The program will take two inputs: an age in Earth years and a number indicating which planet to use for the conversion. Program execution should look like what is shown below. Pay special attention to formatting, spacing, and capitalization of the input and output lines. Enter your age on Earth: 80 Select a planet: 1 for Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune: 2 Your age on Mars is 42.53275109170306 years Enter your age on Earth: 12.5 Select a planet: 1 for Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune: 1 Your age on Mercury is 51.89979538479027 years You can assume that the inputs for age will always be positive numbers, but they may have fractional components. You can also assume that the inputs for selecting a planet will always be one of the integers 1,2,3,4, or 5. No other inputs will be tested. N.B.: you must only use programming structures discussed so far – this means no conditionals and no imported mathematical functions/modules and no object types that we have not discussed yet. You don’t need them here.
  • 38. Program Four: The Taxman If you fail to pay income tax in a timely manner and you owe the government (or the state you live in) money, you are subject both to a penalty and interest on the amount owed. Even if you are living on Mars, if you are a US citizen, you have to file Federal taxes. Suppose that during your stay-cation on Mars, the interstellar mail took too long and you haven’t paid your taxes on time . . . your tax return was literally lost in space. You owe the original tax plus a penalty that is 6% of the tax amount – for our purposes the penalty will always be 6%. You also owe interest on the tax (not the penalty, just the tax). The amount of interest depends on the current yearly rate and the amount of interest you owe depends on how many days overdue the tax is. This is best shown by a few examples. Example 1: Tax owed: $500, 365 days (1 year) overdue
  • 39. Penalty (6% of tax owed): $30 Interest rate: 5% yearly Interest owed (5% of tax owed): $25 Total amount to pay: $555 Example 2: Tax owed: $500, 1095 days (3 years) overdue Penalty (6% of tax owed): $30 Interest rate: 4% yearly Interest owed (4% of tax owed per year times 3 years): $60 Total amount to pay: $590 Example 3: Tax owed: $500, 250 days overdue Penalty (6% of tax owed): $30 Interest rate: 4% yearly Interest owed (4% of tax owed per year times 250 days out of 365): approx.. $13.70 Total amount to pay: $543.70 In the file overdue_tax.py, you have been code for a program that provides constants for the length of a year and the penalty rate, and solicits the tax owed,
  • 40. the number of days overdue, and the interest rate from the user. The program is supposed to calculate the total amount owed, but the program is incorrect. Your job is to debug the program. Debugging the code means that you edit the lines in place. You should not add lines, delete lines, or move the order of lines. Correct the lines as they are and preserve as much of the original code as possible. HINT: figure out what each line is trying to accomplish first before making any changes. The user (and our testing program) will enter the data in the following formats: tax owed will be a string that starts with a dollar sign, e.g., $10000 and interest rate will be a string that is a percentage with a percent sign, e.g. 5%. The days overdue will be entered as an integer. Here are some examples of program execution. Pay special attention to the formats of the inputs and outputs – your printed output should match exactly in terms of spacing and text, but your output may have slightly different numbers of decimal places or differences in the decimal portion and that is OK (e.g. 543.6986301369863 versus 543.6986301369864): Enter amount of tax owed: $500 Enter interest rate: 5% Enter number of days overdue tax is: 365 Your total payment is 555.0
  • 41. Enter amount of tax owed: $500 Enter interest rate: 4% Enter number of days overdue tax is: 1095 Your total payment is 590.0 Enter amount of tax owed: $500 Enter interest rate: 4% Enter number of days overdue tax is: 250 Your total payment is 543.6986301369863 Your program should work for any inputs in the format shown – inputs representing numeric values will always be positive numbers, but keep in mind that tax owed may not be in exact dollar values (i.e. there may be cents like $15000.25 – the IRS usually rounds to the nearest dollar but we won’t) and the interest percentage may not be a whole number and may have more than a single digit (e.g. 10.5%). The number of days will always be input as a positive integer. N.B.: you must only use programming structures discussed so far – this means no conditionals and no imported mathematical functions/modules and no object types that we have not discussed yet. You don’t need them. Extra credit opportunities: Please note that the following tasks are worth a small percentage of extra credit (up to 3%).
  • 42. 1) Program 4 produces outputs that are dollar amounts that may include cents. Format the output so that it prints with two decimal places exactly. It is find to round the second decimal place based on the value of the third decimal place. You may import any modules and use any functions you like to accomplish this. Save this program in a separate file with the name overdue_tax_2.py 2) Write a modified version of Program 3 that allows the user to input their age on a home planet and then converts their age to the selected destination planet. The program will provide the output in the requested format. You must implement this WITHOUT using conditionals. Save this as convert_age_2.py. Here is an of how this should work. This example doesn’t include Earth as an option, but you can include it if you like. Select your home planet: 1 for Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune: 1 Enter your age on your home planet: 10 Select a destination planet: 1 for Mercury, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Neptune:2 Your age on Mars is 1.280494905385735 years
  • 43. SUBMISSION EXPECTATIONS Project1.pdf A PDF document containing your reflections on the project, and information on any extra credit opportunities you pursued. You must also cite any sources you use. Please be aware that you can consult sources, but all code written must be your own. Programs copied in part or wholesale from the web or other sources will receive 0 points and will result in reporting of an Honor Code violation. Earth_to_Mars.py Your implementation of program one, following the format specified above. retirement.py Your implementation of program two, following the format specified above. convert_age.py Your completed implementation of program three. overdue_tax.py Your corrected implementation of program four. Any programs you attempted for extra credit as separate files. Checkpoint submission You may submit any and all parts of the project by Tuesday September 10 to receive feedback from our automated testing script. This is automated feedback only but will show you if your programs have failed any of our test cases. Please be aware that you will receive no feedback if your files are incorrectly named or are in an incorrect format.
  • 44. SUGGESTED COMPLETION SCHEDULE Each project will offer you a suggest completion schedule to help you manage your time. Working incrementally is essential to learning the material, minimizing frustration, and finishing projects by the deadline. This schedule is based on when topics are presented in lecture. This will put you on track to have versions of 3 of the 4 programs to submit by the checkpoint deadline. Earth_to_Mars.py: Complete after lecture 9/2 or 9/3 retirement.py: Start after lecture 9/2 or 9/3, Complete after lecture 9/4 or 9/5 convert_age.py: Complete after lecture 9/9 or 9/10 overdue_tax.py: Complete after lecture 9/4 or 9/5 POINT VALUES AND GRADING RUBRIC Program 1: 15 points Program 2: 27.5 points Program 3: 27.5 points Program 4: 27.5 points
  • 45. Write-up: 2.5 points The general grading rubric for projects is below. Some of the specifics given in the rubric are not applicable to this project (e.g. import lines and copious commenting). Project1/retirement.py amount_to_save = int(input('Enter total amount to save: ')) paychecks = int(input('Enter number of paychecks per year: ')) deduction_times = int(paychecks -1) deduction = amount_to_save//paychecks final_deduction = amount_to_save - deduction_times*deduction print('You must make '+str(deduction_times)+' deductions of $'+str(deduction)+' and one final deduction of $'+str(final_deduction)+' to save $'+str(amount_to_save)) Project1/Write up.doc Project1/Write up.pdf
  • 46. CS140 Professor Willner Wenda Shen In this project, I finished the reading assignment and reviewed the class notes, so that I can attempt the project by using the knowledge we already learned. So far I didn’t look for anything on internet in order to finish the project. This project helps me learn how to differentiate int, float, and strings, and how to refer to a certain position in a list. Moreover, I also learned how to use the correct format to avoid error. CSCI 140 Programming for Data Science Project 2 Checkpoint Deadline 1700, Sunday September 29 Due 1700, Friday, October 4 In our second project, you are tasked with developing functions to simulate playing the lottery. You will create two functions, correct a third function, and write a short main program.
  • 47. N.B.: You must use the structures we have learned in this class to complete this assignment. Implementations using structures we have not covered may receive no credit. For example, you may not use string and list methods since we have not formally talked about methods. We have provided two skeleton files: Project_2.py for your function code, and Project_2_Main.py for your main program. Function One: Generating Lottery Numbers Many states in the United States have a state lottery. Players pick typically between 4 and 6 numbers within a certain range, and then win monetary prizes based on how many of their numbers match numbers randomly drawn in the lottery. For example, a lottery could use the numbers from 1 to 50, and players must pick 5 numbers. Each number can only be chosen once, that is, each number is unique. For example, a player could choose: 39 7 12 14 42 Write a function called generate_numbers that randomly selects lottery numbers. This function has two optional arguments: limit – the maximum number used in the lottery, i.e. the highest number a player can choose, and num – the number of lottery numbers to select. The default value of limit is 34 and the default value of num is 5. These defaults correspond to one of the Virginia lottery games. Your function will return a list of randomly selected lottery numbers. Here are some examples – numbers are selected randomly so these
  • 48. show only 1 possible outcome. • generate_numbers with default values for limit and num Example return value: [20, 10, 34, 12, 31] • generate_numbers with default value for limit, num = 8: Example return value: [16, 14, 12, 28, 26, 15, 30, 32] • generate_numbers with limit = 55, num = 4: Example return value [11, 26, 18, 48] You can assume that the input for limit will always be a positive integer greater than or equal to num. The input for num will be a positive integer. Key points: • Optional argument limit gives the highest possible lottery number that can be chosen; inputs tested will always be correct type • Each lottery number can only be chosen ONCE, they cannot be repeated • Optional argument num is an integer; default value: 5; inputs tested always correct type • Return a list of num integers randomly selected with no duplicates between 1 and limit • You can do this with a loop, but it is actually easier to use one of the functions from the random module without a loop; HINT: how can we generate the numbers from 1 to limit?
  • 49. Function Two:Win or Lose To win the lottery, you must match your numbers to the winning numbers. The prize you win corresponds to how many of your numbers match the winning numbers. The more numbers you match, the larger the prize. For example, if your numbers are: 15 32 42 10 18 and the winning numbers are: 32 42 15 18 9, you have matched four out of five numbers and would win the second largest prize. Typically, you don’t win a prize for matching very few numbers, e.g. in the Virginia lottery, you won’t get anything if you match only one or two numbers to the winning numbers. You have been code for a function called lotto_prize which needs to be corrected. You may not add or delete lines. You may change indentation, but should not change the order of the lines. The function lotto_prize takes three required arguments: attempt – a list containing a player’s lottery numbers, winning – the winning lottery numbers, and prizes – a list containing the prize amounts from largest to smallest. The function will return a numeric value that is the prize won, either an item from prizes or 0. Whether and how much the player wins, and which output is returned, is based on how many numbers match. Matching all numbers gets the largest prize, all but one the second largest, and so forth. If they match too few numbers, they win nothing.
  • 50. The examples below are for demonstration only, your function should PRINT NOTHING. • All of the player’s numbers match: player wins the largest prize attempt is [28, 49, 2, 55, 33, 44], winning is [2, 28, 49, 33, 44, 55] prizes is [1000000, 400, 20, 5] Function returns 1000000 • All but one of the player’s numbers match: player wins the second largest prize attempt is [28, 49, 2, 33, 44], winning is [2, 28, 9, 33, 44] prizes is [1000, 40, 1] Function returns 40 • Player matches too few numbers to win: player wins nothing attempt is [28, 49, 2, 33, 44, 19, 4], winning is [50, 49, 27, 31, 2, 7, 11] prizes is [1250000, 10000, 1500, 50] Function returns 0 The inputs for attempt and winning will always be of the same length, but that length can be any positive integer. The input for prizes is a list sorted from largest to smallest with a length less than or equal to the length of attempt and winning. No other
  • 51. inputs will be tested. Key points: • The text of the code you are given is ALMOST correct and requires only minor changes. These changes mostly have to do with logical errors and things like indexing. You need to preserve as much of the original code as possible. You may need to change indentation for some lines. You do not need to and are not permitted to add any lines or delete any lines. You must understand in detail what each line is meant to do and how to solve the problem on paper. READ THE LAST KEY POINT AND COMMENTS IN CODE VERY CAREFULLY. • All of the required arguments are lists: attempt and winning are the same length and are unsorted; prizes has a length less than or equal to attempt and winning and is sorted largest to smallest - this means the user inputs it sorted, your function does not sort it. • Function returns an integer that is either the prize amount of 0; prize won depends on how many numbers match • The function is written to operate in this general manner: check if each item from attempt is part of winning, count how many items match. Use count of matches to determine if the player wins a prize, and if so, which prize from prizes they
  • 52. win. Return prize amount. Function Three: Can you really win? You decide to play a set of lottery numbers until you win the jackpot, that is until your numbers match all of the winning numbers. How many times would you have to play before this happens? Write a function called simulate_lotto that takes two required arguments: your_numbers –a list containing your lottery numbers and max_pick – the highest number available to pick in the lottery – that is, the highest value that could appear in your numbers or the winning numbers. simulate_lotto also takes an optional argument max_plays which has a default value of 10,000. The purpose of max_plays is to prevent your code from running forever. In a lottery with 6 numbers chosen from 1 to 50, there are 11,441,304,000 different sets of winning numbers, which doesn’t even account for the same numbers being chosen twice. Your program could run for a LONG time before the player’s numbers win. max_plays limits how many times winning numbers will be chosen. Even if the player hasn’t won, the function will stop after max_plays lottery picks. simulate_lotto should return an integer which is the number of plays with your numbers it took to win the jackpot –this means all of the numbers match- OR
  • 53. max_plays, whichever is smaller. Here are some examples to demonstrate how the function works. This is NOT printed OUTPUT. • Example #1: your_numbers is [45, 23, 12, 22], max_pick is 50, max_plays is 10,000 Play 1: winning numbers are [12, 49, 2, 45] Play 2: winning numbers are [7, 8, 41, 13] Play 3: winning numbers are [12, 23, 45, 22] Function returns 3. Notice that the order of the numbers doesn’t matter, just that the winning numbers match your_numbers. Once they match, the function terminates. • Example #2: Example #1: your_numbers is [5, 2, 1], max_pick is 10, max_plays is 4 Play 1: winning numbers are [1, 4, 2] Play 2: winning numbers are [7, 8, 4] Play 3: winning numbers are [1, 3, 4] Play 4: winning numbers are [1,7,9] Function returns 4 – the value of max_plays. Even though the player didn’t win (winning numbers were never [5,2,1]), the function terminates because the max number of plays was reached. Key points:
  • 54. • Input for your_numbers will always be a list of integers. The length of this list can vary. • Input for max_pick will always be a positive integer greater than or equal to the length of your_numbers. Input for max_plays will always be a positive integer. • The winning numbers are generated by your function – they are NOT input by the user. You MUST use your generate_numbers function. Figure out how many numbers to generate from the user input your_numbers – HINT: look at the length of the input. • You MUST use your lotto_prize function to determine whether or not the user won the jackpot. Winning the jackpot means that all numbers match. Think about how to specify the prizes argument to make this work. • You need to use a loop in this function. We have not discussed break or continue. Do not use them. Stop your loop either using logic or by exploiting how return works. MainProgram: Playing the Lottery You will write a brief main program that makes use of your functions. This must be saved and submitted in Project_2_Main.py. The main program must make use of your functions. You will be graded specifically on coding style for this part of the project – that means writing efficient
  • 55. code that is not repetitive, and does not specifying default arguments when possible. The main program should do the following: • Generate five lottery numbers to play where the numbers range from 1 to 65. Use one of your functions to do this. • Generate a second set of five lottery numbers to be the winning numbers. These also range from 1 to 65. Use one of your functions to do this. • Using the two sets of numbers you generated above, calculate the prize the user wins. The available prizes are: $100000 for all matching numbers, $500 for 4 matching numbers, and $50 for 3 matching numbers. Use one of your functions to do this. Print out the prize. • Generate six lottery numbers to play where the numbers range from 1 to 20. Use one of your functions to do this. Simulate playing these numbers until you win the jackpot or you reach 1000 plays. Use one of your functions to do this. Print out how many plays it took. Extra Credit Worth up to 3% total. Fill in under def lines in Project_2_EC.py 1) Many lotteries have a final number drawn that is a “super pick” which has its own prizes and/or
  • 56. can double/triple/etc. the prize won using the other numbers. For example, you might pick 5 regular lottery numbers between 1 and 50 and then pick a final number between 1 and 20: 45 2 5 14 29 Super pick: 11 Fill in the function definition for generate_numbers_super to add support for a single extra “super” pick. Keep in mind that the range of numbers for the super pick may be different from the range for the regular lottery numbers, and this function has an additional parameter super_limit which is the upper bound for the “super pick” (the highest number the super pick can be). The return value is a list where the LAST item in the list is the super pick. 2) Fill in the function definition for lotto_prize_super to add support for a super pick as described in 1. This function has two additional arguments, s_num - a Boolean indicating if the lottery numbers provided indicate a super pick as the last number, and multiplier – this indicates what to multiply the prize by if the user wins. Here’s an example of calling the function: lotto_prize_super([43,57,23,45,1], [40, 45, 32, 23,1],[100000,100, 10], True, 2) In this case, since s_num is True, 1 is separated from the other numbers. The user matched two regular numbers (45 and 23), so they would win $10. Since they also matched the super pick and multiplier is 2, this is doubled and the value returned is 20.
  • 57. 3) The corrected implementation of lotto_prize finds matches a particular way. It might be more efficient to determine matches by sorting the lists first and/or implementing a search algorithm. Or you could implement this function more concisely by using objects other than lists. Fill in the function definition for lotto_prize_ search with any enhancements you can come up with. SUBMISSION EXPECTATIONS Project_2.py Your implementations of functions one, two (corrected), and three. Do not change the names of parameters in the function def lines or the names of the functions – if you do so, it will affect your grade. No additional code including input lines, print lines, and function calls should be submitted. Make sure that you have the correct import lines. If your code requires manual intervention to run such as adding/editing import lines, correcting indentation, removing print and input lines, etc., and/or correcting syntax errors, you can expect up to a 5 point deduction. Project_2_Main.py Your implementation of the main program specified in Part 4. Your functions must not be repeated in this file. They must be imported using the correct import line. Make sure that you have the correct import lines. If your code requires manual intervention, you can expect up to a 5 point deduction. Project_2_EC.py Your implementations of any of the extra
  • 58. credit functions. They must be submitted in this file. No additional code including input lines, print lines, and function calls should be submitted. Make sure that you have the correct import lines. Project_2.pdf A PDF document containing your reflections on the project. You must also cite any sources you use. Please be aware that you can consult sources, but all code written must be your own. Programs copied in part or wholesale from the web or other sources will receive 0 points and will result in reporting of an Honor Code violation. N.B.: You must use the structures we have learned in this class to complete this assignment. Implementations using structures we have not covered may receive no credit. This means NO LIST AND STRING METHODS (yes, that includes append – do not use it). POINT VALUES AND GRADING RUBRIC Function 1: 27.5 points Function 2: 27.5 points Function 3: 32.5 points Main Program: 10 points Write-up: 2.5 points SUGGESTIONS FOR COMPLETION TIMELINE
  • 59. This project integrates concepts from many lectures. The most important step will be to understand what each function does and figure out the necessary steps. Make sure to work things out on paper first. This schedule sets you up to submit all functions for the checkpoint. -Read through instructions and make sure you understand what each function does and how it works by 9/23 – bring questions to office hours and lab; start writing steps on paper ASAP -Complete debugging Function 2 after lecture 9/23-9/24 -Complete Function 1 and first three steps of main program after lecture 9/25-9/26 (you could complete this after lecture 9/23-9/24 but it will be easier with material from 9/25-9/26) -Complete Function 3 and last step of main program after lecture 9/25-9/26 (you could complete this after 9/23-9/24 but it will be easier with material from 9/25-9/26)