Python Excercise

March 17, 2018 | Author: Sazzad Hossain | Category: String (Computer Science), Computer File, Mail, Function (Mathematics), Text File


Comments



Description

Simple exercises designed to get students familiar with the python environment.Python covered:  Int, float, string variables  Input, raw_input  +, -, *, **, /  print 1. Computes the area of a circle with radius 123 cm. 2. Gets the radius of a circle and computes the area. 3. Go to xe.com and find how much a Canadian dollar is worth in Euros (EUR). Make a program that allows the user to enter the amount of Canadian they have and tell them what it is worth in Euros. Gets the user‟s first and last name and outputs them in reverse order with a comma between them. 4. Gets the coordinates for two points from the user and computes the distance. Round your answer to two decimal places. 5. At Jenny‟s birthday party she orders a 32 piece pizza. Have the user (probably Jenny) enter the number of people at the party that will be eating pizza and output the number of slices each one gets. As you know the pizza might not divide evenly. There are two ways to handle this. The first is to ignore the extra pieces and give everyone the same amount. The second way is to cut up the extra pieces so that everyone gets the same amount. Your program must output both options. E.g. Number of guests: 10 Option 1: 3 slices each, 2 left over Option 2: 3.2 slices each There exercises focus on some simple String Manipulation. - Concatenation (+) - Multiplication (*) - Slices - Escape sequences - len() - String Methods  upper  lower  find  replace  count 1. 2. 3. 4. Get the user's full name with one raw_input and display the name as last, first Get a file name from the user and display just the extension. Get a sentence from the user and display it back with one word per line. Get a number from the user (n) and create an n x n box of "X"es on the screen. e.g. If they entered 12: XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX Without using the method center. There exercises focus on some simple Selection  if … elif … else  basic Boolean operators ( >. With what we know now this is not an easy question. "Dogs.93 $1. When building and enclosure for a python the amount of area at the base of the enclosure should be proportionate to the length of the snake. e.25 square feet (6 * ½ + 3 * ¾) Create a program that asks the user how long their python is and tell them the minimum area they need for the base of its enclosure. 4." on either side. In Ontario we have two forms of sales tax PST & GST on fast food.00 or over. PST is 8% but only charged when the price of the meal is $4. accumulators . 9' python needs 5.52 $0. 6. You may assume that the field size is larger than the length of the string. e. Create a program that gets the base price of a meal and outputs the total after tax. The postage you pay to send a letter from Canada depends on where you are sending it. get a string from the user and a field size then display the string centered within that field.g.XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXXXX 5. Write a program that reads in a file name from the user and tells them what kind of file it is by looking at the extension. For a standard sized letter the postage rates are: Mailing within Canada Mailing to USA Mailing Internationally $0. but try it for now.g. There exercises focus on some simple Selection  while  break  counters. Your program must be able to recognize at least 5 different file types. 2. When we learn more this will become easier. == …)  and / or 1.55 Create a program that asks the user where he/she is mailing their letter then tell him/her how much the postage is. with dots ". GST is 6% and always charged on fast food. The minimum needed is 1/2 square foot for each foot in length up to and including 6 and 3/4 square foot for each foot after that.doc" is a Word Document 3. Get a string from the user and display how many capital letters are in the string. Sir John A Macdonald 2. and number. c b) i = 0 while i < 10: n = i ** 2 if i % 2 == 0: print n . 1. Create a program that gets a word from the user and tells them if it is palindrome or not. Write a program to be used by student council in this upcoming election to tabulate the votes after the election. but was not to be pleased by the final payment. He asked for a grain of cereal for the first square and to have each of the next 63 squares double the previous square. a = 20 b = a / 2 c = a . b. a) Trace the following programs. Alexander Mackenzie 3. Enter Vote (0 to exit) : 1 Enter Vote (0 to exit) : 1 Enter Vote (0 to exit) : 1 Enter Vote (0 to exit) : 2 Sir John A Macdonald won Sir John A Macdonald earned 75% of the votes Alexander Mackenzie earned 25% of the votes Sir John Abbott earned 0% of the votes 4. Tell them how many classes they are failing (despite what many of you think a fail is 50%) As you know the inventor of the game chess was quite a clever fellow. assume there are 7000 grains to a pound. Sir John Abbott 3. the three candidates for president then allow the user to enter all of the ballets until they enter 0 then tell them who won the election and what percentage each candidate earned. Your program should not care about capitalization. As the story goes the inventor presented the game to the king and the king was quite pleased. Your program will list. As the story goes the king agrees.1. e. 2. The king asked what the inventor wanted as payment. Express your answer in pounds. Create a program that computes how much grain the king had to pay.g.b if b < c: print "unfair" elif a > b: print "easy as ABC" print a. Palindromes are words that are the same forwards and backwards. Create a program that gets the marks the user has for their classes this year. 1. I paid each of them $12. I need your program to let the user type in eight kids names and how many weeds each of 2. This will not be a detailed look at all of the weird and wonderful ways of using these types but instead a basic summary of what they are and HOW to use them. it works 100: * 2 * 2 * 2 .{„McKenzie‟ : 149.) For each friend in the list randomly choose a word and another friend in the list. 3. “banana”.py n1 = "Vincent" n2 = "Massey" ans = "" i = 0 while i < len(n2): ans = ans + n1[i] + n2[i] n1 = n1[2:] n2 = n2[2:] print ans Create the program: This is an introduction to some more advanced types.10 x. fruit = [“apple”.c) x = y = z while z < x = x y = x z = y print = 1 # trust me.g.[1. “orange”. “misses” . „Annet‟ : 125.y. Use these two lists to create an attractive “fruit stand” menu. I had my eight nieces and nephews over to my house in the summer to pick weeds. 4] Tuple .g.. „Safranyos‟ : 105} These exercises focus on the nuts and bolts of how lists work. Make a list of ten fruits and another list of the price each fruit. “loves”. 3. because I hate doing it myself. I'm still going to pay $100 in total but this time how much each person gets will be based on how many weeds they pulled. fruit[1:3] 1.„McKenzie‟ Dictionary . . List . 2.50 but it bothered me that some of them gave it their all and some of them spent more time with the Super-Soakers than they did pulling weeds. Write a program to help me out. 4) String . These data types are fundamental to Python. 2.(1.. “hates”. he probably does anyways. Even the programs from last section are already using tuples without the student‟s knowledge. 3.z d) # traceEx4. fruit[2]. Highlights:  creating a list e. Make a list of five of your friends and a list of five emotions or action words (e. “pear”]  Slices: e.g. Don't worry if “Jack loves Jack”. I've decided that next summer I'm going to pay based on performance.. 2. Your computerized version will look something like: Player 1 hit enter to roll You rolled: 3 You are at spot 3 Player 2 hit enter to roll You rolled: 6 You climbed a ladder You are at spot 17 although you can write this program without using lists. Create a function called hypotenuse that calculates the hypotenuse of a right-angle triangle given the other two sides. These exercises focus on creating and using our own functions:  def . . The rules of the game are simple.defining a function  parameters  return values  calling a function 1. Snakes and Ladders can be a fun game for kids to play but you realize when you get older that the players actually have no choices at all. To illustrate just how little input the players have I want you to make a computer program that allows two players to play snakes and ladders on the board given.py 4. If you are near the end and your roll would cause you to go past the end square you don't move for that turn. A palindrome is a word that reads the same both forward and backwards.the kids pulled then print out a list of how much each kid has earned. (the percentage of the $100 they get should be based on the percentage of the pile of weeds that they picked) save as listEx3. If they end their move at the base of a ladder then they automatically climb to the top of the ladder. To win the game you must be the first player to land on the last square. On each player's turn they roll one six sided die and move ahead that many squares. If they end their move at the end of a snake they automatically slide down to its head. you should ask yourself how you can use lists to encode where the snakes and ladders are. Create a function called palindrome that takes a single word as a parameter and returns True if the word is a palindrome and False otherwise. draw. 20 x 20 rectangles at random positions on the screen. These exercises are designed to increase familiarity with using pygame to draw basic graphics. Knights in chess move in an L shape. Create a program that draws 1000 filled circles of 3. Create a program that draws your scene in python Create a program that draws an attractive black and white chess board. The persistence of any other number is one plus the persistence of the digits multiplied together. col) of all possible places the knight can move.circle  RGB Colours 1. This question is fairly hard. 6. col) of a knight on the chess board and returns a list of tuples (row. 5.rect. Create a program that draws a simple 2-d pyramid of circles on the screen. Use graph paper to draw a scene (e. Draw a pattern of lines from the center of the screen to every 10th pixel on the perimeter of the screen. 5. What you may not have noticed is that persistence is inherently recursive.) Determine which python shapes you would need to draw this on the computer and write the important coordinates on the paper.line. draw. 2. 4. as the diagram shows.g.3. I expect you to plan it out on paper first.  pygame template developed in class  draw. The persistence of any one-digit number is zero. radius 3 at random positions within 200 pixels of the center of . As you know the persistence of a number is the number of steps it takes to get a one-digit number when separating the digits and multiplying them together. Create a recursive function called persistence that computes the persistence of any whole number. They go 2 in one direction then one space at a 90 angle. Create a function called knightMove that takes in the current location (row. your house. The base should have 15 circles and yes this involves some math. Create a program that draws 100. ) Create a program that allows the user to enter a class then have your program open a file with that class name and output the names of everyone who is in that class into the file. Make a program that reads from both of these files and displays a nice table that shows the names. This list will keep track of the top 10 best scores along with the number that was being guessed.the screen. writelines() methods 1. In your class folder are two files "stocks. Write a program that plays the high-low guessing game with the user. Also randomly determine the colour of each circle separately. Creating the functional programming: . but only call them by their first name save as fileEx1. When trying to identify the author of a piece of work it is sometimes useful to examine the frequency that words are used. Read the file "story.txt". if the user entered "ICS3M-01" your program would create and fill a file called "ICS3M-01. The value is the value of one share of the stock. 2. Your output file will simply be each word in the story with how many times it occurred. Draw two randomly placed radius 10 circles on the screen then draw radius 2 circles every twenty pixels from the center of one to the center of the other. These exercises are designed to increase familiarity with the basic syntax of reading and writing with files. In your class folder is a file called "names.txt" Create a program that reads from the names file and simply says hello to each person. sorta.txt". readlines() methods  write(). 7. close() method  read(). Each stock has: <name> <code> <value> Each of these are separated by tabs. it has a list of the stocks that I own in the form of: <code> <num> Where num is the number of shares of the stock I own.TXT". stocks is a file that has a list of stocks in it. Your program must keep track of a high score list.txt" it is a list of classes of everyone in the school (well. and an overall total. 5.txt" and write to a file called "frequency.txt" and "stockProfile. This list should be sorted from most to least frequent. So. read(1) readline(). 4.py In your class folder is a file called "classes.  open() function. and values of each stock I own. stockProfile contains a list of my personal stock profile. 3.
Copyright © 2024 DOKUMEN.SITE Inc.