Word Jumble Game

e-papa 0 Tallied Votes 660 Views Share

This is a simple word jumble game, I actually adapted it from one I used from practice.
It's written in Python 3.1 using the pyscripter. NO modules needed.

# Word Jumble
#
# The computer picks a random word and then "jumbles" it
# The player has to guess the original word
#
# Obasa Adegoke - 11/04/11(dd/mm/yy)

import random


#this function jumbles the word
def word_jumble(word):
    """takes a word as arguement and jumbles it"""
    jumble=''
    while word:
        position=random.randrange(len(word))
        jumble+=word[position]
        word=word[:position] + word[position+1:]
    return jumble

#this is a function that selects a sequence from a tuple
def picker(w):
    """selects a random word from a tuple"""
    word=random.choice(w)
    return word

#this are the tuples contaning the word
terms = ('designing', 'programming', 'development', 'analysis', 'coding')
company = ('microsoft', 'IBM', 'apple', 'intel', 'google')
languages = ('java', 'cold fusion', 'python', 'jython', 'javascript', 'perl', )
systems = ('software', 'hardware', )
name = ('gates', 'jobs', 'yang', 'trovalds', 'filo')

#the various tuples are stored as a sequences in another tuple
combine = (terms, company, languages, name, systems)

#this function will prompt fo action to the taken after program execution
def decision():
    """prompts user for decision after program execution"""
    decide = input('Do you want to try again (Y/N)')
    decide = decide.upper()
    if decide == 'Y':
        run()
    elif decide == 'N':
        quit()
    else:
        print('It\s either Y or N')
        decision()

#this is the function that runs the program
def run():
    """this function runs the program"""
    word = picker(picker(combine))
    jumble = word_jumble(word)
    print(jumble)
    guess = (input('Your guess\n'))
    while guess != word:
        print('You\'re wrong, try again')
        guess = (input('Your guess\n'))
    print('You\'re right')
    decision()

#contents are now being seen from here
print('-----------------------------------------------------------------------')
print('---------------------------Word Jumble Game----------------------------')
print('---------------------------by adegoke obasa----------------------------')
print('----------------------------Instructions-------------------------------')
print('      A certain word pertaning to computer science has been jumbled, \n\
                 You\'re are to guess the word that is jumbled')
print('-----------------------------------------------------------------------')

run()
e-papa 13 Posting Pro in Training

I'm very sorry for the mistake in the Poll, that was suppose to Good, Please accept my apologies.

e-papa 13 Posting Pro in Training

Please guys, comment on this code snippet, to know what I need to improve on.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

word_jumble(word) == ''.join(random.sample(word, len(word))) Better to put all decision in one while True: instead of deeper and deeper recursion to decision().
You can center text by center() method, it has optional parameter for fill character:

>>> 'Word Jumble Game'.center(71,'-')
'----------------------------Word Jumble Game---------------------------'

You can avoid escaping ' by \ if you use " as outside quote. You can include line changes inside ''' or """ quotes (any single quotes inside OK):

print('''A certain word pertaning to computer science has been jumbled,
You're are to guess the word that is jumbled''')
e-papa commented: Good. +1
woooee 814 Nearly a Posting Maven

I thought you wanted a goose (but it won't come from me). Why do you call "picker" twice?

def run():
    """this function runs the program"""
    word = picker(picker(combine))

Also, you can reach the maximum recursion level since run() calls decision() which calls run(), etc. And there is more recursion in decision()

def decision():
    """prompts user for decision after program execution"""
    decide = input('Do you want to try again (Y/N)')
    decide = decide.upper()
    if decide == 'Y':
        run()
    elif decide == 'N':
        quit()
    else:
        print('It\s either Y or N')
        decision()
#
#--------------------------------------------------------------------
    decide = input('Do you want to try again (Y/N)')
    decide = decide.upper()
    while decide not in ["N", "Y"]:
        print("It's either Y or N")
        decide = input('Do you want to try again (Y/N)')
        decide = decide.upper()
    return decide  ## to run, decide is now "N" or "Y"

Then return the "decide" variable to run(), which will use it to test a while loop for "Y".

e-papa 13 Posting Pro in Training

Thanks, will try to implement.

e-papa 13 Posting Pro in Training

Please are you saying that once i call picker() it will automatically select a tuple and then make another selection from that tuple.

e-papa 13 Posting Pro in Training

I've tried this

#this is the function that runs the program
def run():
    """this function runs the program"""
    word = picker(combine)
    jumble = word_jumble(word)
    print(jumble)
    guess = (input('Your guess\n'))
    while guess != word:
        print('You\'re wrong, try again')
        guess = (input('Your guess\n'))
    print('You\'re right')
    decision()

but it fails to pick a single word from the tupe, and rather picks the entire tuple, so I think this

#this is the function that runs the program
def run():
    """this function runs the program"""
    word = picker(picker(combine))
    jumble = word_jumble(word)
    print(jumble)
    guess = (input('Your guess\n'))
    while guess != word:
        print('You\'re wrong, try again')
        guess = (input('Your guess\n'))
    print('You\'re right')
    decision()

is okay

Will try the others too.

e-papa 13 Posting Pro in Training
# Word Jumble
#
# The computer picks a word in computer science and then "jumbles" it
# The player has to guess the original word
#
# Obasa Adegoke - 11/04/11(dd/mm/yy)

import random


#this function jumbles the word
def word_jumble(word):
    """takes a word as arguement and jumbles it"""


    jumble=''
    while word:
        position=random.randrange(len(word))
        jumble+=word[position]
        word=word[:position] + word[position+1:]
    return jumble

#this is a function that selects a sequence from a tuple
def picker(w):
    """selects a random word from a tuple"""
    word=random.choice(w)
    return word

#this are the tuples contaning the word
terms = ('designing', 'programming', 'development', 'analysis', 'coding')
company = ('microsoft', 'IBM', 'apple', 'intel', 'google', 'AMD', 'DELL')
language = ('java', 'ruby', 'python', 'jython', 'javascript', 'perl', )
sub_systems = ('software', 'hardware')
inventor = ('gates', 'jobs', 'yang', 'trovalds', 'filo')

#the various tuples are stored as a sequences in another tuple
combine = (terms, company, language, inventor, sub_systems)

#this function will prompt fo action to the taken after program execution
def decision():
    """prompts user for decision after program execution"""


    decide = input('Do you want to try again (Y/N)')
    decide = decide.upper()
    while decide not in ["Y", "N"]:
        print('----------------'.center(70))
        print("It's either Y or N".center(70))
        decide = input('Do you want to try again (Y/N)')
        decide = decide.upper()
    return decide


#this is the function that runs the program
def run():
    """this function runs the program"""

    section=picker(combine)
    word = picker(section)
    jumble = word_jumble(word)
    print('----------------'.center(70))
    print(jumble.center(70))
    print('----------------'.center(70))
    guess = (input('Your guess\n'))
    while guess != word:
        print('You\'re wrong, try again'.center(70))
        guess = (input('Your guess\n'))
    print("You're right, it's %s".center(70) %(guess))
    answer = decision()

    if answer == "Y":
        print('----------------'.center(70))
        print("Here we go again".center(70))
        run()

    elif answer == "N":
        print('----------------'.center(70))
        print("Thank you".center(70))
        quit()

#contents are now being seen from here
print('-'.center(70,'-'))
print('Word Jumble Game'.center(70,'-'))
print('by adegoke obasa'.center(70,'-'))
print('Instructions'.center(70,'-'))
print("A word pertaining to computer science has been jumbled".center(70,'*'))
print("You're are to guess the word that is jumbled". center(70,'*'))
print('-'.center(70,'-'))

run()

This is the snippet repackaged, from the contributions of wooeee and tonyjv.
Thanks guys.
Please run and let me know how good.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

I formatted it little of excessive noise in output and here is that my suggestion of shuffle to jumble word. Infinity recursive calls replaced with 'while True's.

# Word Jumble
#
# The computer picks a word in computer science and then "jumbles" it
# The player has to guess the original word
#
# Obasa Adegoke - 11/04/11(dd/mm/yy)

import random


#this function jumbles the word
def word_jumble(word):
    """takes a word as arguement and jumbles it"""
    return ''.join(random.sample(word, len(word)))


#this is a function that selects a sequence from a tuple
def picker(w):
    """selects a random word from a tuple"""
    return random.choice(w)

#this are the tuples contaning the word
terms = ('designing', 'programming', 'development', 'analysis', 'coding')
company = ('Microsoft', 'IBM', 'Apple', 'Intel', 'Google', 'AMD', 'DELL')
language = ('Java', 'Ruby', 'Python', 'Jython', 'Javascript', 'Perl', )
sub_systems = ('software', 'hardware')
# Finnish guy's name is Linus Torvalds, What is 'filo'?
inventor = ('Gates', 'Jobs', 'yang', 'Torvalds', 'filo')


#the various tuples are stored as a sequences in another tuple
combine = tuple(word for section in (terms, company, language,
                                     inventor, sub_systems)
                for word in section)


def run():
    """this function runs the program"""
    while True:
        word = picker(combine)
        jumble = word_jumble(word)
        print('  '.join(jumble).center(30, '*'))
        while True:
            guess = input('Your guess (Q to quit, ! to give up): ').lower()
            if guess in tuple('q!'):
                print(("You did not guess that it was %r!" % word)
                        if guess == '!'
                        else 'Bye, bye!')
                break
            elif guess != word.lower():
                print("You're wrong, try again!")
            else:
                print("You're right, it's %s!" % word)
                break
        if guess == 'q':
            print("\n\tThank you\n".center(66, '*'))
            break

#contents are now being seen from here
print(70 * '-')
print('Word Jumble Game'.center(40, '*').center(70))
print('by adegoke obasa'.rjust(74))
print('Instructions'.center(40, '*').center(70))
print("A word pertaining to computer science has been jumbled.".center(70))
print("You're are to guess the word that is jumbled". center(70))
print(70 * '-')

run()

You might consider adding checking of not repeating words and getting hint on topic of the word.

woooee 814 Nearly a Posting Maven

Your picker code
word = picker(picker(combine))
first picks a tuple, then a word from the picked tuple. While this works, our "debug sensors" were alerted as this would not be obvious down the road when someone is debugging the code. A more accepted version is to combine all of the tuples and pick a random word from the combination by calling picker() once.

terms = ('designing', 'programming', 'development', 'analysis', 'coding')
company = ('Microsoft', 'IBM', 'Apple', 'Intel', 'Google', 'AMD', 'DELL')
language = ('Java', 'Ruby', 'Python', 'Jython', 'Javascript', 'Perl', )
sub_systems = ('software', 'hardware')
inventor = ('Gates', 'Jobs', 'yang', 'Torvalds', 'filo')
combine = terms + company + language + sub_systems + inventor
print combine
nezachem 616 Practically a Posting Shark

> What is 'filo'?

Probably Filo.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

> What is 'filo'?

Probably Filo.

I am more familiar with LIFO
and yang must be http://en.wikipedia.org/wiki/YANG, even I never heard of it before ;) .

e-papa 13 Posting Pro in Training

David filo, is one of the guys that started yahoo.

e-papa 13 Posting Pro in Training

Your picker code
word = picker(picker(combine))
first picks a tuple, then a word from the picked tuple. While this works, our "debug sensors" were alerted as this would not be obvious down the road when someone is debugging the code. A more accepted version is to combine all of the tuples and pick a random word from the combination by calling picker() once.

terms = ('designing', 'programming', 'development', 'analysis', 'coding')
company = ('Microsoft', 'IBM', 'Apple', 'Intel', 'Google', 'AMD', 'DELL')
language = ('Java', 'Ruby', 'Python', 'Jython', 'Javascript', 'Perl', )
sub_systems = ('software', 'hardware')
inventor = ('Gates', 'Jobs', 'yang', 'Torvalds', 'filo')
combine = terms + company + language + sub_systems + inventor
print combine

Okay I understand now, and I think it understandable too.
Thanks for the comments, I've really gained a lot.

e-papa 13 Posting Pro in Training

I formatted it little of excessive noise in output and here is that my suggestion of shuffle to jumble word. Infinity recursive calls replaced with 'while True's.

# Word Jumble
#
# The computer picks a word in computer science and then "jumbles" it
# The player has to guess the original word
#
# Obasa Adegoke - 11/04/11(dd/mm/yy)

import random


#this function jumbles the word
def word_jumble(word):
    """takes a word as arguement and jumbles it"""
    return ''.join(random.sample(word, len(word)))


#this is a function that selects a sequence from a tuple
def picker(w):
    """selects a random word from a tuple"""
    return random.choice(w)

#this are the tuples contaning the word
terms = ('designing', 'programming', 'development', 'analysis', 'coding')
company = ('Microsoft', 'IBM', 'Apple', 'Intel', 'Google', 'AMD', 'DELL')
language = ('Java', 'Ruby', 'Python', 'Jython', 'Javascript', 'Perl', )
sub_systems = ('software', 'hardware')
# Finnish guy's name is Linus Torvalds, What is 'filo'?
inventor = ('Gates', 'Jobs', 'yang', 'Torvalds', 'filo')


#the various tuples are stored as a sequences in another tuple
combine = tuple(word for section in (terms, company, language,
                                     inventor, sub_systems)
                for word in section)


def run():
    """this function runs the program"""
    while True:
        word = picker(combine)
        jumble = word_jumble(word)
        print('  '.join(jumble).center(30, '*'))
        while True:
            guess = input('Your guess (Q to quit, ! to give up): ').lower()
            if guess in tuple('q!'):
                print(("You did not guess that it was %r!" % word)
                        if guess == '!'
                        else 'Bye, bye!')
                break
            elif guess != word.lower():
                print("You're wrong, try again!")
            else:
                print("You're right, it's %s!" % word)
                break
        if guess == 'q':
            print("\n\tThank you\n".center(66, '*'))
            break

#contents are now being seen from here
print(70 * '-')
print('Word Jumble Game'.center(40, '*').center(70))
print('by adegoke obasa'.rjust(74))
print('Instructions'.center(40, '*').center(70))
print("A word pertaining to computer science has been jumbled.".center(70))
print("You're are to guess the word that is jumbled". center(70))
print(70 * '-')

run()

You might consider adding checking of not repeating words and getting hint on topic of the word.

Thanks for this I'll try to analyze and implement, I already thought of hints, but I don't know how you can get the name of a tuple to print or return, I think I'll try a dictionary.
I'm really sorry about the "torvalds", it's a mistake on my part. I apologize. The not repeating words I will really have to sit down with. THANKS AGAIN.

e-papa 13 Posting Pro in Training

Thanks woooee, i tried combining all the tuples into a single a tuple, it worked just fine. Thanks.

e-papa 13 Posting Pro in Training

I have been able to adjust the program, based on the recommendations, here it is

# Word Jumble
#
# The computer picks a word in the IT world and then "jumbles" it
# The player has to guess the word
#
# Obasa Adegoke - 11/04/11(dd/mm/yy)

import random


#this function jumbles the word
def word_jumble(word):
    """takes a word as arguement and jumbles it"""

    jumble = ''
    while word:
        position = random.randrange(len(word))
        jumble += word[position]
        word = word[:position] + word[position+1:]
    return jumble

#this is a function that selects a sequence from a tuple
def picker(w):
    """selects a random word from a tuple"""

    word = random.choice(w)
    #this line returns the word in uppercase
    return word.upper()

#this are the tuples contaning the words
terms = ('designing', 'programming', 'algorithm', 'debugging', 'coding')
company = ('microsoft', 'IBM', 'apple', 'intel', 'nokia', 'AMD', 'DELL')
company2 = ('cisco', 'acer', 'lenovo', 'compaq', 'toshiba', 'hp' )
social = ('facebook', 'myspace', 'youtube', 'blogger', 'wordpress' )
mail = ('gmail', 'yahoomail', 'hotmail', 'aol')
language = ('java', 'ruby', 'python', 'jython', 'HTML', 'perl', 'CSS' )
sub_systems = ('software', 'hardware')
inventor = ('gates', 'jobs', 'yang', 'torvalds', 'filo', 'zukerberg')
search_engines = ('google', 'excite', 'bing', 'vivisimo')

#the various tuples are stored as a single tuple
combine = terms + company + language + inventor + sub_systems + social
combine += company2 + mail + search_engines


#this function will prompt fo action to the taken after program execution
def decision():
    """prompts user for decision after program execution"""

    decide = input('Are you sure you want to quit (Y/N)')
    decide = decide.upper()
    while decide not in ["Y", "N"]:
        print('*'.center(40, '*').center(70))
        print("It's either Y or N".center(70))
        decide = input('Are you sure you want to quit (Y/N)')

#this is the function that runs the program
def run():
    """this function runs the program"""

    #picks the word to be jumbled, jumbles it and outputs it
    word = picker(combine)
    #this line creates a hint for the user
    hint = ("starts with %s, ends with %s" %(word[0], word[-1])).center(70)
    jumble = word_jumble(word)
    print()
    print(jumble.center(70))
    print()

    #interaction with the user starts here
    while True:

        #this lines accepts input and makes sure it's uppercased
        guess = (input('Your guess (Q to quit, G to give up)\n')).upper()
        if guess in ('Q', 'G'):
            print(("You couldn't guess that it was %s" %word).center(70)
                if guess == 'G'
                else "Too bad, you quit".center(70))
            break
        elif guess != word:
            print(("Oops, try again").center(70))
            print(hint)
        else:
            print(("You're right, it's %s" %word).center(70))
            break
    if guess == "Q":
        decision()
        print("Thank you" .center(70))

#contents are now being seen from here
#decorators
print('-'.center(40,'-').center(70))
#introducing the program
print('Word Jumble Game'.center(40,'*').center(70))
print('by adegoke obasa'.center(40,'*').center(70))
print('Instructions'.center(40).center(70))
print("A word in the IT world has been jumbled".center(40).center(70))
print("Guess the word". center(40).center(70))
print("To quit [Q], To give up [G]". center(40,'*').center(70))
print()

#the program actually starts to run here
run()

#decorators
print('-'.center(40,'-').center(70))
#year of production
print('word jumble v1.0'.center(70))
print("copyright 2011".center(70))

Thanks again, your reviews will be highly welcome.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

Looks like your code runs only until first correct answer.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

And decision have no efect as it has no return value and if it had, it would not be used in line 87.

e-papa commented: I think I get it. +1
e-papa 13 Posting Pro in Training

Looks like your code runs only until first correct answer.

Yes it does, I've been looking into a way I could correct that, but nothing for now, i will see what i can do.
Thanks again.

e-papa 13 Posting Pro in Training

And decision have no efect as it has no return value and if it had, it would not be used in line 87.

I think i should just move it into the run function, so has to reduce recursion, am i right.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.