first python program

Soldato
Joined
6 Mar 2008
Posts
10,079
Location
Stoke area
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? :rolleyes:

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 :D

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. :)
 
Thanks for the feedback :)

listLength was from another way of approaching the idea that didn't work out, I forgot to delete it and you're right, 3 letters don't need to be jumbled so that's a minor change again.

With your feedback and that over on the python forum i've joined it looks like what started out as a basic program might have room to grow into something a lot bigger than I thought.

As you say, punctuation is an issue I need to deal with. Also, some words just aren't readable after being jumbled so need to look at that. Someone else has pointed out that :

I have a list of 41K English words I use for passwords. I checked it for words that could be scrambled into each other and found 183 pairs and 2 triplets.

So that needs looking at.

I wasn't aware of PEP 8 so i'll look in to that and make changes to function name styling.

I've not checked the spoiler yet, want to try rewriting using 'shuffle' instead of 'sample' :D

I'll post back when I get time to redo it :) thanks again.
 
Back
Top Bottom