Python Workshop Exercises. Exercises

Pages 7
Views 40
of 7
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Description
Python Workshop Exercises Baiju Muthukadan ZeOmega, Bangalore FOSSMeet'14, NIT Calicut Feb 15, 2014 Exercises Exercise 1 Write a Python program to Hello, World! and save this in a file named helloworld.py.
Transcript
Python Workshop Exercises Baiju Muthukadan ZeOmega, Bangalore FOSSMeet'14, NIT Calicut Feb 15, 2014 Exercises Exercise 1 Write a Python program to Hello, World! and save this in a file named helloworld.py. Make this program executable and run it like:./helloworld.py Exercise 2 Write a Python program (ex2.py) to swap values of two variables. Exercise 3 Write a program that asks for two numbers. If the sum of the numbers is greater than 100, That is a big number. Exercise 4 Write a program that asks the user their name, if they enter your name say That is a nice name , if they enter John Cleese or Michael Palin , tell them how you feel about them ;), otherwise tell them You have a nice name. Exercise 5 Rewrite the below program (ex5.py) to have a separate function for the area of a square, the area of a rectangle, and the area of a circle (3.14 * radius ** 2). This program should include a menu interface. # By Amos Satterlee def hello(): 'Hello!' def area(width, height): return width * height def _welcome(name): 'Welcome,', name name = raw_input('your Name: ') hello(), _welcome(name) 'To find the area of a rectangle,' 'enter the width and height below.' w = input('width: ') while w = 0: 'Must be a positive number' w = input('width: ') h = input('height: ') while h = 0: 'Must be a positive number' h = input('height: ') 'Width =', w, 'Height =', h, 'so Area =', area(w, h) Exercise 6 Expand the ex6.py program given below so it has a menu giving the option of taking the test, viewing the list of questions and answers, and an option to quit. Also, add a new question to ask, What noise does a truly advanced machine make? with the answer of ping . ## This program runs a test of knowledge # First get the test questions # Later this will be modified to use file io. def get_questions(): # notice how the data is stored as a list of lists return [[ What color is the daytime sky on a clear day? , blue ], [ What is the answer to life, the universe and everything? , 42 ], [ What is a three letter word for mouse trap? , cat ]] # This will test a single question # it takes a single question in # it returns True if the user typed the correct answer, otherwise False def check_question(question_and_answer): # extract the question and the answer from the list question = question_and_answer[0] answer = question_and_answer[1] # give the question to the user given_answer = raw_input(question) # compare the user's answer to the testers answer if answer == given_answer: Correct return True Incorrect, correct was: , answer return False # This will run through all the questions def run_test(questions): if len(questions) == 0: No questions were given. # the return exits the function return index = 0 right = 0 while index len(questions): # Check the question if check_question(questions[index]): right = right + 1 index = index + 1 # go to the next question index = index + 1 # notice the order of the computation, first multiply, then divide You got , right * 100 / len(questions),\ % right out of , len(questions) # now let's run the questions run_test(get_questions()) Exercise 7 Rewrite the below program (ex7.py) to use a random integer between 0 and 99 instead of the hard-coded 78. Use the Python documentation to find an appropriate module and function to do this. # Plays the guessing game higher or lower number = 78 guess = 0 while guess!= number: guess = input( guess a number: ) if guess number: Too high elif guess number: Too low Just right Answers Answer 1 1. Content of helloworld.py: #!/usr/bin/env python Hello, World! 2. Change mode from shell: $ chmod +x helloworld.py 3. Run program and verify output like this: $./helloworld.py Hello, World! Answer 2 1. Content of the file ex2.py: x, y = 2, 3 x, y = y, x x, y 2. Run program and verify output like this: $ python ex2.py 3 2 Answer 3 1. Content of the file ex3.py: number1 = input('1st number: ') number2 = input('2nd number: ') if number1 + number2 100: 'That is a big number.' 2. Run program and verify output like this: $ python ex3.py 1st number: 56 2nd number: 78 That is a big number. Answer 4 1. Content of the file ex4.py: name = raw_input('your name: ') if name == 'Ada': 'That is a nice name.' elif name == 'John Cleese' or name == 'Michael Palin': 'Wow. that\'s a great name!' 'You have a nice name.' 2. Run program and verify output like this: $ python ex4.py Your name: Ada That is a nice name. $ python ex4.py Your name: John Cleese Wow. that's a great name! $ python ex4.py Your name: Jack You have a nice name. Answer 5 1. Content of the file ex5.py: def square(length): return length * length def rectangle(width, height): return width * height def circle(radius): return 3.14 * radius ** 2 def : Options: s = calculate the area of a square. c = calculate the area of a circle. r = calculate the area of a rectangle. q = quit This program will calculate the area of a square, circle or rectangle. choice = x while choice!= q : choice = raw_input( please enter your choice: ) if choice == s : length = input( length of square: ) The area of this square is , square(length) elif choice == c : radius = input( radius of the circle: ) The area of the circle is , circle(radius) elif choice == r : width = input( width of the rectangle: ) height = input( height of the rectangle: ) The area of the rectangle is , rectangle(width, height) elif choice == q : , Unrecognized option. Answer 6 1. Content of the file ex6.py: ## This program runs a test of knowledge questions = [[ What color is the daytime sky on a clear day? , blue ], [ What is the answer to life, the universe and everything? , 42 ], [ What is a three letter word for mouse trap? , cat ], [ What noise does a truly advanced machine make? , ping ]] # This will test a single question # it takes a single question in # it returns True if the user typed the correct answer, otherwise False def check_question(question_and_answer): # extract the question and the answer from the list question = question_and_answer[0] answer = question_and_answer[1] # give the question to the user given_answer = raw_input(question) # compare the user's answer to the testers answer if answer == given_answer: Correct return True Incorrect, correct was: , answer return False # This will run through all the questions def run_test(questions): if len(questions) == 0: No questions were given. # the return exits the function return index = 0 right = 0 while index len(questions): # Check the question if check_question(questions[index]): right = right + 1 # go to the next question index = index + 1 # notice the order of the computation, first multiply, then divide ( You got , right * 100 / len(questions), % right out of , len(questions)) #showing a list of questions and answers def showquestions(questions): q = 0 while q len(questions): a = 0 Q: , questions[q][a] a = 1 A: , questions[q][a] q = q + 1 # now let's define the menu function def menu(): Menu: 1 - Take the test 2 - View a list of questions and answers 3 - View the menu 5 - Quit choice = 3 while choice!= 5 : if choice == 1 : run_test(questions) elif choice == 2 : showquestions(questions) elif choice == 3 : menu() choice = raw_input( choose your option from the menu above: ) Answer 7 1. Content of the file ex7.py: from random import randint number = randint(0, 99) guess = -1 while guess!= number: guess = input ( Guess a number: ) if guess number: Too high elif guess number: Too low Just right Please write your 2 minute feedback here:
Advertisements
Related Documents
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks