Python Workshop Exercises
Python Workshop Exercises
Baiju Muthukadan
ZeOmega, Bangalore
FOSSMeet'14, NIT Calicut
Exercises
Exercise 1
Write a Python program to print "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, print "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
print
def hello():
print 'Hello!'
def print_welcome(name):
print 'Welcome,', name
h = input('Height: ')
while h <= 0:
print 'Must be a positive number'
h = input('Height: ')
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".
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:
print "Correct"
return True
else:
print "Incorrect, correct was:", answer
return False
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.
number = 78
guess = 0
Answer 1
1. Content of helloworld.py:
#!/usr/bin/env python
$ chmod +x helloworld.py
$ ./helloworld.py
Hello, World!
Answer 2
1. Content of the file ex2.py:
x, y = 2, 3
x, y = y, x
print x, y
$ python ex2.py
3 2
Answer 3
1. Content of the file ex3.py:
$ python ex3.py
1st number: 56
2nd number: 78
That is a big number.
Answer 4
1. Content of the file ex4.py:
$ 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 circle(radius):
return 3.14 * radius ** 2
def options():
print
print "Options:"
print "s = calculate the area of a square."
print "c = calculate the area of a circle."
print "r = calculate the area of a rectangle."
print "q = quit"
print
print "This program will calculate the area of a square, circle or rectangle."
choice = "x"
options()
while choice != "q":
choice = raw_input("Please enter your choice: ")
if choice == "s":
length = input("Length of square: ")
print "The area of this square is", square(length)
options()
elif choice == "c":
radius = input("Radius of the circle: ")
print "The area of the circle is", circle(radius)
options()
elif choice == "r":
width = input("Width of the rectangle: ")
height = input("Height of the rectangle: ")
print "The area of the rectangle is", rectangle(width, height)
options()
elif choice == "q":
print "",
else:
print "Unrecognized option."
options()
Answer 6
1. Content of the file ex6.py:
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"]]
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:
print "Correct"
return True
else:
print "Incorrect, correct was:", answer
return False
def run_test(questions):
if len(questions) == 0:
print "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
print ("You got", right * 100 / len(questions),
"% right out of", len(questions))
choice = "3"
while choice != "5":
if choice == "1":
run_test(questions)
elif choice == "2":
showquestions(questions)
elif choice == "3":
menu()
print
choice = raw_input("Choose your option from the menu above: ")
Answer 7
1. Content of the file ex7.py: