You are on page 1of 1

def char_count(word,char):

""""This function counts the number of times a character appears in a word.


Note that this function uses for loops instead of while loops. Pleas e-mail if this
function doesn't work: aabyrd@wisc.edu"""
counter = 0
charCounter = 0
for element in word: #Set my for loop, which explains what container char
goes into
if element == char: #Create condition for loop will follow
charCounter += 1
counter += 1
return charCounter

def are_anagrams(str_1,str_2):
""""This function tests if two strings have the same number of characters. If
they do, this function will identify both strings as an anagram."""
for character in str_1: #set for loop for variable character in first string
input
if character.isalpha(): #check the characters that are alpha-letters,
skip space
if char_count(str_1,character) == char_count(str_2,character):
pass
else:
return False
for character in str_2: #Copy the loops and conditions from above to check
str_2
if character.isalpha():
if char_count(str_1,character) == char_count(str_2,character):
pass
else:
return False
return True

def main():
"""This main function brings in the are_anagram functions to continually ask
for new inputs. In other words, the function will ask you to provide to inputs and
then it will test if the words are anagrams, and then ask for more inputs
automatically"""
while True: #set the loop so that it's always True. Will always ask for new
inputs
str_1 = raw_input('String 1:')
str_2 = raw_input('String 2:')
str_1.lower() #returns a new string if all are lowercase
new_str1 = str_1.lower() #create a variable to store new string
new_str2 = str_1.lower() #create a variable to store new string
if are_anagrams(new_str1, new_str2): #calling function are_anagrams
print "Anagrams!"
else:
print "Not anagrams."

if __name__ == "__main__":
main()