Soldato
ok, well, I've done it, my first python program. I've been through the lessons on codeacademy, covers basics and functions. Normally I learn the basics and never use them, and then forget it. However I came up with a little fun idea at work after seeing something on facebook.
It was one of those images where the first and last letters of each word are the same but all the others are jumbled up. I thought it couldn't be that hard right?
I've used 3.2 to do this with, first time I've used it and took some getting used to the tiny changes.
Things to note: this is a first draft, still has some things I want to work out like making sure the jumbled word isn't the same as the word that was input. I know there will be better ways of doing this, purely because a) I'm new to it and b) I've not covered a lot of this so had to google a lot and figure out what the hell it was about
So, ways to improve it? how would you have done it in Python?
Obviously I want to add user input as well.
It was one of those images where the first and last letters of each word are the same but all the others are jumbled up. I thought it couldn't be that hard right?
I've used 3.2 to do this with, first time I've used it and took some getting used to the tiny changes.
Things to note: this is a first draft, still has some things I want to work out like making sure the jumbled word isn't the same as the word that was input. I know there will be better ways of doing this, purely because a) I'm new to it and b) I've not covered a lot of this so had to google a lot and figure out what the hell it was about
Code:
# This software will take a sentence, leave the first and last letters the same
# but jumble up the rest. It will then print the word like this: "pirnt" (print)
# TO DO:
# wordJumble needs to make sure the output isn't the same as the input.
import random
def wordJumble(word): # jumbles a word
if len(word) <= 2:
return word
else:
rest = word[1:-1]
restLen = len(rest)
jumbled = random.sample(rest,restLen)
reentry = (''.join(jumbled))
complete = word[0] + reentry + word[-1]
return complete
def complete(sentence):
wordList = sentence.split() # splits sentence in to a list
listLength = len(wordList) # finds the number of words in the list
total = ""
for word in wordList:
total = total + " " + (wordJumble(word))
return total
print (complete("Anyone who knows anything of history knows that great social changes are impossible without feminine upheaval. Social progress can be measured exactly by the social position of the fair sex, the ugly ones included."))
So, ways to improve it? how would you have done it in Python?
Obviously I want to add user input as well.