Skip to content

Instantly share code, notes, and snippets.

@safehammad
Created April 23, 2013 09:58
Show Gist options
  • Save safehammad/5442313 to your computer and use it in GitHub Desktop.
Save safehammad/5442313 to your computer and use it in GitHub Desktop.
The beginnings of a Koans framework for interactively teaching Python. Created during a coding session at Python Northwest in April 2013. Much more to do :)
#!/usr/bin/env python3
def introduction():
print('Welcome to the Python teacher. My job is to teach you how to program.')
def learn_print_literal():
"""
The 'print' function in Python is used to print words. You only need to tell
it the words that you want to print! 'print' is passed a sentence inside brackets,
like 'print('some words').
"""
user_input = input('Try it for yourself: ')
try:
eval(user_input)
except SyntaxError:
print('Sorry, that is not the way to print in Python. Try again!')
return False
else:
print('Congratulations, that is the right answer!')
return True
def learn_print_variable():
"""
A variable is a piece of information stored by a name, i.e. 'x'
Variables can also be printed, i.e. print(x)
I have already created a variable called 'x' for you and given it a value:
If you print the variable, you'll see what value it has
"""
x = 'message'
user_input = input('Try it for yourself: ')
try:
eval(user_input)
except SyntaxError:
print('Sorry, that is not the way to print a variable in Python. Try again!')
return False
else:
print('Congratulations, that is the right answer!')
return True
if __name__ == '__main__':
introduction()
questions = [
learn_print_literal,
learn_print_variable,
]
for question in questions:
print(question.__doc__)
while not question():
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment