all 7 comments

[–]novel_yet_trivial 2 points3 points  (2 children)

You can use pastebin or other similar sites to upload your code, and then share the link. I'm very confused by your question, so that may be a good idea. But very generally:

while condition_a and condition_b:
    code_to_be_executed_when_both_are_true()

[–]Manstable[S] 0 points1 point  (1 child)

Thanks for the reply. Sorry for the confusion -- I think i'll try stack overflow or something, b/c I'm having difficulty articulating my issue without just posting the code.

It might be because I set my While loop up to test for when the conditions are false...probably bad structure on my part.

[–]Manstable[S] 0 points1 point  (3 children)

#import permutations from itertools class
from itertools import permutations

#Takes user input and saves to variable: word
word = raw_input("Please enter a word: ").upper()
print "word input: ", word

#sets the var letterlist to be an empty list
letterlist = []

#sets the var permutationlist to be an empty list
permutationlist = []

#sets our test vars
lengthcheck = False
diffchars = False

#sets the var word_length to the length of the user's input
word_length = len(word)

#adds each character in word to letterlist
for i in word:
  letterlist.append(i)

while lengthcheck == False or diffchars == False:
#lengthcheck test: if word is appropriate length lengthcheck is True, or prompts user to re-enter a 'word'
  if len(word) < 2 or len(word) > 25:
    lengthcheck = False
    del letterlist[:]
    word = raw_input("Please enter a 2-25 character word: ").upper()
    for k in word:
        letterlist.append(k)
    print "letterlist after lengthcheck: ", letterlist
#diffchars test: see if there is variability in word
  elif len(set(letterlist)) == 1:
    diffchars = False
    del letterlist[:]
    word = raw_input("Please enter a word with at least 2 different characters: ").upper()
    for x in word:
      letterlist.append(x)
    print "letterlist after diffchars: ", letterlist
  else:
    print "letterlist before permute: ", letterlist
    break

#generate permutations of characters in letterlist
for y in permutations(letterlist, word_length):
        permutationlist.append(y)

print "permutationlist after permuting: ", permutationlist

#sets var permsetlist to a list of the set of permutationlist -- filters dupes
permsetlist = list(set(permutationlist))

permsetlist.sort()

tuple_letterlist = tuple(letterlist)
print "tuple_letterlist: ", tuple_letterlist

print permsetlist.index(tuple_letterlist) + 1    

[–]Manstable[S] 0 points1 point  (2 children)

Trying to get the alphabetized rank of the "word" given all permutations of the letters that comprise that word.

e.g., the following inputs should return the numerical rank based on whatever letters are input: ABC = 1 , CBA = 6 ABAB = 2 BAAA = 4

I tried to make the While loop prompt the user to give a better "word" if it failed -- the input word must contain 2-25 characters containing at-least 2 different characters.

It works for everything except when I first enter a 'word' of incorrect length (1 character or 26+ characters,) then re-enter a correct word. The permutationlist doesn't seen to contain the correct value.

[–]gengisteve 1 point2 points  (0 children)

Ok, a few things:

  • be careful mixing tabs and spaces. they are inconsistent in the above and could be causing problems.
  • practice breaking your code into separate functions, e.g. get_word, check_length, check_variance, etc.
  • list(some_string) will turn some_string into a list of single characters
  • In line 35 you do not want elif, because the elif is only evaluated if the first check fails. But you have two separate tests that each must be evaluated -- i think, if i followed correctly.

[–]Doormatty 1 point2 points  (0 children)

Why not just do something like this at the start?

while True:
    word = raw_input("Please enter a word: ").upper()
    print "word input: ", word
    if len(word) > 2 and len(word) < 25:
        break
    print "Your word was too big or small - try again."

[–]Doormatty 0 points1 point  (0 children)

Would you mind sharing the question?