first python program

Soldato
Joined
6 Mar 2008
Posts
10,080
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. :)
 
Looks fun. Little projects like this are undoubtedly the best way to learn :)

A few points:
- As you say, wordJumble can return the same word as input.
- You've filtered out <=2 letter words, but 3 letter ones are also not going to jumble.
- s.split() returns punctuation with a word, hence the last letter (to not be jumbled) may be a '.' or ',' etc.
- you never actually use listLength, and overall the code is quite verbose.

Lastly, this is purely stylistic, and please feel free to ignore it if you prefer. Under PEP 8, camelCaseForFunctionNamesIsABitFrownedUpon and lowercase_names_with_underscores_are_preferred. Virtually everyone has their own way of doing things, but as you're only just starting out, you could take it into consideration. Hell, I do a lot of things against PEP 8 myself :o

A version that deals with the above is below, but have a go yourself before looking :p

Code:
import random, string

def word_jumble(word):
    if len(word) <= 3:
        return word
    elif len(word) == 4 and word[-1] in string.punctuation:
        return word
    else:
        if word[-1] in string.punctuation:
            word, punc = word[:-1], word[-1]
        else:
            punc = ''
        jumbled = word
        while jumbled == word:
            jumbled = word[0] + ''.join(random.sample(word[1:-1], len(word) - 2)) + word[-1]
        return jumbled + punc

def complete(sentence):
    return ' '.join([word_jumble(word) for word in sentence.split()])
 
Last edited:
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