Home » PythonPi Bonus Lessons
Category Archives: PythonPi Bonus Lessons
PythonPi Bonus: Knock Knock
Today we are going to be playing with user input but not really paying much attention to what is said in the user input. We will be using the user input to move on to the next part of the program. This will take part by being the response in a given knock knock joke.
For example, a knock knock joke would go:
Computer: Knock Knock
User: Who’s there
Computer: Doctor
User: Doctor Who
Computer: Exterminate!
To make a program that will accept input as anything, because we aren’t going to compare anything, the user may reply anything to each part of the joke, we just want to print out the next part of the joke, each time the user responds:
print ‘Knock Knock’
varInput = raw_input()
print ‘Doctor’
varInput = raw_input()
print ‘Exterminate’
PythonPi Bonus: 99 Bottles Of Beer
For this program, you will need to create a loop to create the popular song.
99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around. 98 bottles of beer on the wall.
Consider how to insert an int (number variable) to a string and how to reduce the value of the bottles of beer whilst in the loop. This should be a fairly simple exercise and is a good demonstration of while loops.
PythonPi Bonus: Fibonacci
When carrying out the bonus lessons, you should have already completed the 8 week introductory course and submitted all your coursework to maximum possible standard.
Today we will be learning about the vast mathematical functions of Python starting with Fibonacci numbers. Fibonacci is a sequence of numbers that add together with the previous one in the sequence to create the next one. For example:
0,1,1,2,3,5,8,13,21,34,55
That is an initial Fibonacci sequence starting from Zero. In Python we can write a very easy function to do this, where we define the number in the sequence we want, and Python will return us the number in that section of the sequence. The code is fairly simple to write as well.
First of all we will write a function that allows us to ask it for the fibonacci number we require.
def fibonacci(n):
if n == 0:
return 0;
elif n == 1:
return 1;
else:
return (fibonacci(n-1)+(fibonacci(n-2))
HINT: input_var returns a string, you’d want an int to pass to the method above.
Call this function and then insert the number you would like to find in the fibonacci sequence. You can do this in the Virtual Programming Lab in Classroom and earn an extra badge for doing more coding 😀
Recent Comments