HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Guessing words from scrambled letters

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
wordsscrambledguessingfromletters

Problem

How could I possibly shorten / clean this up? I suppose mainly clean up the loop at the start asking whether they would like to scramble the code.

def userAnswer(letters):
    print("Can you make a word from these letters? "+str(letters)+" :")
    x = input("Would you like to scrample the letters? Enter 1 to scramble or enter to guess :")
    while x == '1':
        print(''.join(random.sample(letters,len(letters))))
        x = input("Would you like to scrample the letters? Enter 1 to scramble or enter to guess :")
    word = input("What is your guess? :")
    word = word.lower()              
    if checkSubset(word, letters) == True and checkWord(word) == True:
        print("Yes! You can make a word from those letters!")
    else:
        print("Sorry, you cannot make that word from those letters")

userAnswer("agrsuteod")

Solution

Before presenting my version, I would like to make a couple of comments:

  • Use descriptive names. The name userAnswer gives me an impression of just getting the user's answer, nothing else. I like to suggest using names such as startScrambleGame, runScrambleGame or the like.



  • Avoid 1-letter names such as x -- it does not tell you much.



  • I don't know which version of Python you are using, but mine is 2.7 and input() gave me troubles: it thinks my answer is the name of a Python variable or command. I suggest using raw_input() instead.



  • Your code calls input() 3 times. I suggest calling raw_input() only once in a loop. See code below.



  • The if checkSubset()... logic should be the same, even if you drop == True.



Here is my version of userAnswer, which I call startScrambleGame:

def startScrambleGame(letters):
    print("Can you make a word from these letters: {}?".format(letters))
    while True:
        answer = raw_input('Enter 1 to scramble, or type a word to guess: ')
        if answer != '1':
            break
        print(''.join(random.sample(letters, len(letters))))

    word = answer.lower()              
    if checkSubset(word, letters) and checkWord(word):
        print("Yes! You can make a word from those letters!")
    else:
        print("Sorry, you cannot make that word from those letters")

startScrambleGame("agrsuteod")

Code Snippets

def startScrambleGame(letters):
    print("Can you make a word from these letters: {}?".format(letters))
    while True:
        answer = raw_input('Enter 1 to scramble, or type a word to guess: ')
        if answer != '1':
            break
        print(''.join(random.sample(letters, len(letters))))

    word = answer.lower()              
    if checkSubset(word, letters) and checkWord(word):
        print("Yes! You can make a word from those letters!")
    else:
        print("Sorry, you cannot make that word from those letters")

startScrambleGame("agrsuteod")

Context

StackExchange Code Review Q#24508, answer score: 6

Revisions (0)

No revisions yet.