Is this scrapbook for you?
The topics I cover in this scrapbook start at a very basic level which progress to more complicated later on. When I started writing this scrapbook (2015) my skill level was roughly on par with this program.
#!bin/python2.7
# Palindrone
# A palindrone is an alphanumeric string or statement that is the same forwards and backwards.
# reverse a string
def reverse_my_string(x):
"""Reverse all letters of a string"""
return x[::-1]
def ask_for_input():
""" Ask the user for a word """
# treat all user input as a string
word = raw_input("Enter a word and press enter: ")
return word
def palindrone():
"""Determine if a word is a palindrone."""
word = ""
while len(word) <= 1:
try:
# ask user for input
word = ask_for_input()
# make sure the word has 2 or more characters
len(word) >=2
except:
print 'Your word needs to be at least two characters'
# reverse the users word
word_backwards = reverse_my_string(word)
# is the word a palindrone
if word == word_backwards:
print 'Congratulations, your word [%s] is a palindrone' %word
else:
print 'Sorry, [%s] is not the same as [%s]' %(word, word_backwards)
def main():
""" Give the user 3 entries and stop """
counter = 0
# loop from 0 to 2
for i in range(3):
# Entry count
counter = counter + 1
print 'Entry %s' %counter
# call palindrone function
palindrone()
# run
main()
# output
# Entry 1
# Enter a word and press enter: test one
# Sorry, [test one] is not the same as [eno tset]
# Entry 2
# Enter a word and press enter: Hello World!
# Sorry, [Hello World!] is not the same as [!dlroW olleH]
# Entry 3
# Enter a word and press enter: god dog
# Congratulations, [god dog] is a palindrone
Update: heres how I would write it in 2018
Note that I have switched to Python 3 in this example.
"""
Determine if a word is a palindrone.
A palindrone is an alphanumeric string or
statement that is the same forwards and backwards.
Run at the terminal using -a to denote the number
of attempts to give the user.
python3 palindrone.py -a 3
"""
def testPalindrone(w):
"""
Determine if a word is a palindrone.
args
----
w: string
Represents a word to reverse
returns
-------
Boolean
example
-------
>>> palindrone('test')
False
"""
# reverse word
word_backwards = w[::-1]
# test palindrone
if w == word_backwards:
return True
else:
return False
def main(attempts):
"""
Give the user n attempts and stop.
args
----
attempts: integer
Represents the number of goes to give the user.
example
-------
main(1)
Entry 1
Enter a word with more than 2 letters and press enter: d
words must be longer than 2 letters
Entry 1
Enter a word with more than 2 letters and press enter: done
Sorry, done is not the same as enod
Entry 2
Enter a word with more than 2 letters and press enter: level
Congratulations, your word level is a palindrone
Entry 3
Enter a word with more than 2 letters and press enter: palindrone
Sorry, palindrone is not the same as enordnilap
"""
counter = 1
# attempts+1 from 0 index
while counter < attempts+1:
print('\nEntry {}'.format(counter))
# ask for word
word = input("Enter a word with more than 2 letters and press enter: ")
# test palindrone
if len(word) > 2:
counter = counter + 1
palindrone = testPalindrone(word)
if palindrone:
print('Congratulations, your word {} is a palindrone'.format(word))
else:
print('Sorry, {} is not the same as {}'.format(word, word[::-1]))
else:
print('words must be longer than 2 letters')
continue
if __name__ == '__main__':
"""
Run in a terminal
flags
-----
--attempts (-a)
Number of attempts to give the user
example
-------
python3 palindrone.py -a 3
"""
from optparse import OptionParser
parser = OptionParser()
parser.add_option(
"-a",
"--attempts",
dest="attempts",
help="Number of words to attempt as palindrone",
metavar="ATTEMPT"
)
(options, args) = parser.parse_args()
# Run main func
main(int(options.attempts))