- StudyBlue
- Alabama
- University of Alabama - Tuscaloosa
- Computer Science
- Computer Science 150
- Wiegand
- project 0

charles h.

Advertisement

CS 150 Programming I Project 0 Revision Date: January 5, 2009 This is your rst Python assignment. You may develop your code on your USB-stick (or anywhere else), but you must ensure it runs correctly on your stick before submission. Statistics! There are lies, damned lies, and statistics { Benjamin Disraeli Your task is to prompt the user of your program to enter ve distinct numbers and report the following statistics about those ve numbers: the mean the median the standard deviation the number of integers entered the number of real numbers entered the number of even integers entered the number of odd integers entered the number of positive numbers entered the number of negative numbers entered At this point, you are to ask the user of your program if he or she wishes to play a game. If the user answers in the a rmative, ask the user to write down ve random (or not) digits down on a piece of paper. Then ask the user to write down the digits in a di erent order and to subtract the smaller number from the larger number. Tell the user to circle one of the digits in the result, but not to circle a zero, since a zero is already a circle (this trick depends on the user not selecting a zero). Now ask the user to enter the remaining digits in the answer, in any order. Inform the user that he or she made your task too easy and announce the circled number. To calculate the circled number, simply add up the entered digits and determine what number added to the sum would make the sum divisible by nine. That additional number is the circled number. Program Organization Name your main python le statistics.py. Provide two more les named stats.py and game.py, respectively. The statistics.py le should look something like this: from stats import * from game import * def main(): print(?Welcome to the Statistician Magician!?) a,b,c,d,e = getNumbers() print(?the mean is ?,mean(a,b,c,d,e)) print(?the median is ?,median(a,b,c,d,e)) # ... # ... answer = input(?Say, would you like to play a game? ?) if affirmative(answer): game() print(?Have a nice day!?) main() The le stats.py should contain your mean, median, etc. functions. The le game.py should have your game function and other supporting functions. For example, here is the mean function: def mean(a,b,c,d,e): return (a + b + c + d + e) / 5 which should be placed in your stats.py le. You are free to make up your own greeting and dialog. Getting the numbers Here is how to get a number and make the variable m point to it: m = eval(input("Give me a number: ")) To have a function return multiple values, simply separate the values by commas: return m,n,o,p,q 2 Finding the Median The median value of a collection of distinct values is that value for which the number of items in the collection less than the value are equal to the number of items greater than that value. A simple way to nd the median value of ve numbers is to collect those values into something known as a list, sort the list, and then pull out and return the middle value. For example, to put two values a and b into a list named items, one would issue the command: items = [a,b] To sort the items, pointing the variable sItems to the sorted list, one would issue the command: sItems = sorted(items); To pull out the rst and second values in the list pointed to by sItems, one would use bracket notation: first = sItems[0] second = sItems[1] Your task is to de ne a function that takes ve arguments, place the arguments into a list, sorts the list, and return the middle value of the sorted list. Finding the Standard Deviation To calculate the standard, you will need to total up a series of calculations. Here?s how to total up the rst ve squares: total = 12 + 22 + 32 + 42 + 52 In python, this could be written as: def squareSum(): total = 0 total = total + pow(1,2) total = total + pow(2,2) total = total + pow(3,2) total = total + pow(4,2) total = total + pow(5,2) return total You should model your standard deviation-calculating function after squareSum. Even and odd numbers Here is a function that tells you whether a given number is odd. It return True if the number was odd and False otherwise. 3 def isOdd(x): return x % 2 != 0 For this project, only integers can be even or odd. Real numbers are neither. Integers versus Reals Here is a function that tells you whether a given number is an integer or not: def isInteger(x): return type(x) == type(0) A Little More Math To nd out what to add to a number x to make it divisible by nine, use the modulus operator: remainder = x % 9 Now take the remainder and subtract it from 9. That gives you what you need to add to x to make it divisible by nine. Here?s an example. Suppose x is 22. Then x % 9 is 4, since 9 goes into 22 evenly twice with a remainder of 4. Subtracting 4 from 9 leaves 5. So 22 + 5 should be divisible by 9, which it is. Compliance Instructions To make sure that you have implemented your program correctly, save this le: test0. you should be able to run the following command: cat test0 | python statistics.py and see output similar to the following: Welcome to the Statistician Magician! Enter the first number: Enter the second number: Enter the third number: Enter the fourth number: Enter the fifth number: Submission Instructions Change to the directory containing your assignment. If you are working on your USB-stick, run the command: 4 submit This will produce the following output: Usage: submit and will remind you of the order of the arguments to submit. You will now submit your assignment with the command: submit cs150 xxxx project0 Note that project0 ends in a zero, not a capital O. Replace xxxx with your instructor?s nixie login name. When you run the script, make sure there are no errors. You may need to scroll back through the output of the script. You will be asked for the nixie guest password, which your instructor should have given to you. Due Date The due date for this assignment can be found on the class schedule. 5

Advertisement

"The semester I found StudyBlue, I went from a 2.8 to a 3.8, and graduated with honors!"

Jennifer Colorado School of Mines
StudyBlue is not sponsored or endorsed by any college, university, or instructor.

© 2014 StudyBlue Inc. All rights reserved.

© 2014 StudyBlue Inc. All rights reserved.