Hi all,

I am new to programming, and i'm working from a booik which has set me a challenge with a random jumble word program to pair the jumble word with a hint. I have been going round in circles trying to work it out, please help point me in the right direction.

Here is my code:

import random

name = ("stephen","bob", "robert","bill")
word = random.choice(name)
correct = word
jumble = ""

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

print("\nThe jumble word is: ",jumble)
guess = input("\nTake a guess: ")

while guess != correct and guess !="":
    print("\nYour guess is wrong: ")
    guess = input("\nTake another guess: ")

if guess == correct:
    print("\nWell done you have guessed correctly.")
    print("\nGoodbye.")





input("\nPress the enter key to exit.")

Thanks very much i will take a look. The book i'm working from Python programming for the absolute beginner by Micheal Dawson

You could do something like this ...

import random

name = ("stephen", "bob", "robert", "bill")
word = random.choice(name)
correct = word
jumble = ""

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

print("\nThe jumble word is: ",jumble)
guess = input("\nTake a guess: ")

count = 0
while guess != correct and guess !="":
    print("\nYour guess is wrong: ")
    count += 1
    # add hints as the wrong guess count advances ...
    if count == 1:
        print("\nThe first letter of the correct word is {}".format(correct[0]))
    elif count == 2:
        print("\nThe second letter of the correct word is {}".format(correct[1]))
    guess = input("\nTake another guess: ")

if guess == correct:
    print("\nWell done you have guessed correctly.")
    print("\nGoodbye.")

input("\nPress the enter key to exit.")

Edited 2 Years Ago by vegaseat

Thanks very much vegaseat, i like the way this one adds the extra letter as a hint after each attempt.I have not come across the curly brackets yet on this line of code

print("\nThe first letter of the correct word is {}".format(correct[0]))

can you please explain.

.I have not come across the curly brackets yet on this line of code

New string formatting came out in Python 2.6
Gribouillis has much about it here
A quick look at at old and new.

>>> old = 'My name is %s' % 'foshan'
>>> old
'My name is foshan'
>>> 
>>> new = 'My name is {}'.format('foshan')
>>> new
'My name is foshan'

I think Python doc is usaually is ok,but with new string formatting not so good.
Here a coulpe of good link.
http://ebeab.com/2012/10/10/python-string-format/
http://www.cs.cmu.edu/~nschneid/pythonathan/py-format-string-ref.pdf

Edited 2 Years Ago by snippsat

This question has already been answered. Start a new discussion instead.