Create a program that takes in the price of one US dollar or less for an item from the user then calculates the appropriate change to give back to the user, assuming that they always pay $1. You may assume that the user always enters an integer, however, if they enter a number that is under 1 or over 100, your program should display a message telling the user that they have entered an incorrect purchase price and exit.
In summary:
● Ask the user to enter a number 1 – 100
● If the user does, calculate the change to give them:
○ Display the change using the fewest number of pennies, nickels, dimes, and quarters. Do not display coin denominations that are not needed.
● If the user does not enter a number 1 – 100, inform them that they have entered a bad input and do not execute any more code.
For this program, you’ll code a simulation that repeatedly rolls three 6-sided dice. You’ll be calculating how often the three dice all have different values. If you’ve taken a discrete math or probability course (by no means required!), you may have learned that the theoretical probability of obtaining three different values is
6 × 5 × 4 ≅ 0. 5556. This program will calculate the experimental probability of getting three different values for 63
rolling the three dice 10 times, 100 times, 1000 times, and 10000 times.
Consider the case when you roll the dice 10 times. Each time my while loop iterates, you want to roll all three dice once and answer the question “are these all different?”.For this question you will use the random library.
To import the package:
# import the package and give it the nickname “rnd” import random as rnd
A few useful functions:
● rnd.randrange(min,max) – Generate and return a random integer from min to max-1 ● rnd.randint(min,max)-Generate and return a random integer from min to max
rnd.random()- Generate and return a random float from 0.00 to 1.00
For this question you will again use the random library. A squirrel starts at position 0. Each second, the squirrel moves either left (pos = pos – 1) or right (pos = pos + 1) with equal probability. Plot the position of the squirrel as a function of time over 1000 seconds. (Time is the x-axis, and the squirrel’s position is plotted along the y-axis.) Be sure to label your axis and include an appropriate title.
Here are a few plotting functions that you may find helpful:
● importmatplotlib.pyplotasplt-notaplottingfunction,butyou’llneedtoimportthematplotlib
library to use its plotting functions!
● plt.plot(xcoordinate,ycoordinate,markertype,color=”colorcode”)-addasingle
point to your graph, optionally pass in a color code.
○ Marker type documentation: https://matplotlib.org/stable/api/markers_api.html
○ Color code documentation:
https://matplotlib.org/stable/gallery/color/named_colors.html#css-colors
● plt.xlabel(stringforthelabelofthex-axis)-setthex-axislabelforyourgraph
● plt.ylabel(stringforthelabelofthey-axis)-setthey-axislabelforyourgraph
● plt.title(stringforthetitleofthegraph)-setthetitleforyourgraph
● plt.savefig(filenametosaveto,bbox_inches=”tight”)-saveyourgraphasafileonyour
computer. bbox_inches = “tight” makes it so that there isn’t a lot of extra whitespace included around
your graph.
● plt.show()-afunctionthattellspython”I’mdonemakingthecurrentgraph”anddisplaysit.
Make sure to save your output as walk.png and include this image with your homework submission!Question 4 (walk2d.py)
For this question, you will create an updated version of your previous program. Now with each second, the squirrel is walking in two dimensions. Starting at the origin (at position (x=0, y=0)), the squirrel now moves one step randomly in any of four directions, NORTH, SOUTH, EAST, or WEST. (They cannot move diagonally.) However, to make it a little more interesting, the squirrel is twice as likely to move in the EAST-WEST directionrather than the NORTH-SOUTH direction.
Use the color = “color-code” parameter to pick a single color for all of your points. Example output updated 9/26/2020