0
\$\begingroup\$

A code where the goal is to get as many words as possible from one string. The rules are strict but users can cheat easily because this is not an English dictionary.

import random

WORDS = ('treehouse', 'python', 'learner','Michael Jackson', 'phenomenon', 
'December', 'television', 'pathophysiological', 'technological advancements', 
'schizophrenia', 'immunodeficiency', 'photosynthesis')


def prompt_for_words(challenge, player_number):

    guesses = set()
    print('\nWhat words can you find in {} Player {}?'.format(challenge.upper(), player_number))
    print('(Enter Q to quit)\n')

    while True:
        guess = input('{} words >   '.format(len(guesses)))

        if guess.upper() == 'Q':
            break
        elif not guess.isalpha():
            continue

        list_challenge = list(challenge)
        valid = True

        for letter in guess:
            if letter not in challenge:
                valid = False
                break
            elif ' ' in list_challenge:
                del list_challenge[list_challenge.index(' ')]

                if list_challenge.count(letter) < list(guess).count(letter):
                    valid = False
                    break
            elif list_challenge.count(letter) < list(guess).count(letter):
                valid = False
                break

        if len(guess) == len(challenge):
            continue
        elif valid:
            guesses.add(guess.lower())
        else:
            continue

    return guesses


def output_results(result):

    for word in result:
        print('        ' + word)


challenge_word = random.choice(WORDS)
player1_words = prompt_for_words(challenge_word, 1)
player2_words = prompt_for_words(challenge_word, 2)

print('\nPlayer 1 Results:')
player1_unique = player1_words.difference(player2_words)
print('    {} guesses, {} unique'.format(len(player1_words), 
len(player1_unique)))
output_results(player1_unique)

print('\nPlayer 2 Results:')
player2_unique = player2_words.difference(player1_words)
print('    {} guesses, {} unique'.format(len(player2_words), 
len(player2_unique)))
output_results(player2_unique)

print('\nShared guesses:')
shared_guesses = player1_words.intersection(player2_words)
print('    {} shared guesses'.format(len(shared_guesses)))
output_results(shared_guesses)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.