Jump to content

Help! Trying to jumble a users input

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Sp32

Sp32

    Newbie

  • Members
  • PipPip
  • 26 posts
Hey I think you might be able to see what I am trying to do here, here is the code I have came up with

import random
print "hello this is the decypher\n"
persons_word = raw_input("Enter your word: ")
print persons_word.random.randrange()
raw_input("Press the enter key to quit")


I am pretty sure the random.randrange is where I am going wrong


What I am trying to do is get a word from the user and jumble it up so if they typed something like this: "galaxy" python will return several words what "galaxy" can make for instance

"galayx"
"algyxa"

if you see what I am trying to do and know how to help please respond!

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
You're on the right track. The function you're looking for is not random.randrange, but random.shuffle. It'll do the job for you.
import random
word = list(raw_input("Enter a word: "))
random.shuffle(word)
word = ''.join(word)
print "The jumbled word: %s" % word
Notice that we're working with lists. random.shuffle takes a list, so we need to convert our user input from string => list. When we've shuffled the word, we uses join to make the list => string.

#3
Sp32

Sp32

    Newbie

  • Members
  • PipPip
  • 26 posts
Thanks for your help but looking at this makes me think I still need to learn alot!

I have never head of .shuffle or .join and not sure what the

%s" % word

bit does

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts

"Sp32" said:

I have never head of .shuffle or .join
shuffle is a function in the object random. random is just a class, which contains data, functions, etc. And so are join, but it's not in the object random, but in str. shuffle shuffles a list in random order, and join puts a list into a concrete object, like a string.

"Sp32" said:

I have never head of .shuffle or .join and not sure what the

%s" % word

bit does
It's an easier way to make long messages, containing values, variables, and so on. You could have been using the +operator too, but I prefer this way. One thing you've to remember is, when there's more parameters to the string, then you have to close them in parentheses.
print "Integer: %d" % one_parameter
print "Integers: %d and %d" % (one_parameter, two_parameters)
As you see it looks better, than the following. It's also easier to handle, when you're working with bigger messages.
print "Integer: " + one_parameter
print "Integers: " + one_parameter + " and " + two_parameters


#5
Sp32

Sp32

    Newbie

  • Members
  • PipPip
  • 26 posts
Thanks for your help and your time, I was using the + sign cause the book I am learning from told me about it :]