4 Replies - 1660 Views - Last Post: 21 November 2011 - 09:51 AM Rate Topic: -----

#1 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Random key from dictionary

Posted 21 November 2011 - 12:24 AM

Hello all. I have a school assignment to create a game that scrambles a word and allows the user to guess what they think the word is etc..

It works fine and meets all criteria. the problem I am having is when I try to loop the program back through it gives the same exact word from the dictionary as before, I want to to choose a different word the next time it loops.

this is what I have

thanks all!


def programIntro():
    print('I will show you a scrambled word and give you three chances to')
    print('guess the original word. After your second incorrect guess, I will help')
    print('by providing a hint.')
    print()
    print('Here we go ....')


def ranWord():
    word = random.choice(list(dictionary.keys()))
    return word


def scramble():
    daList = list(randomWord)
    random.shuffle(daList)
    print (''.join(daList))


    

import random

dictionary = { 
'tuna': 'A fish',
'spoon': ' eat soup with this', 
'shoe' : ' wear these on your feet', 
'jacket': ' put these on during winter',
'book': ' read these', 
'cup': ' drink out of these',
'keyboard': 'use these to type',
'watch': 'what time is it', 
'processor': 'the central part of a computer',
'key': 'unlock things with it', 
'paper': 'organic writing surface', 
'ninja': 'if seen it is too late for life', 
'wallet': 'hold money in this', 
'speaker': 'sound comes from these',
'dinosaur': 'prehistoric badasses', 
'dog': 'barks a lot', 
'chair': 'makes sitting a pleasure', 
'phone': 'allows you to instantly talk with people', 
'sushi': 'a food from the gods discovered by the Japanese', 
'printer': 'prints things', 
'RAM': 'computer memory', 
'wrench': 'a tool for tightening bolts', 
'switch': 'turns things on or off', 
'bicycle': '2 wheeled man powered vehicle', }

randomWord = ranWord()


playAgain = 'y'
while playAgain == ('y'):
    programIntro()
    print ('The scrambled word is... ')
    scramble()
    loop = 0
    while loop < 3:
        loop = loop +1
        guess = input('what is your guess for guess number ' + str(loop) + ': ' )
        print()
        if guess == randomWord:
            print ('Good work, your guess is correct')
            break
            
            

        if guess != randomWord:
            print ('Sorry, that is not correct')
        if loop == 2:
            print("Here's a hint ----->> " + (dictionary.get(randomWord,[])) ) 
            print()

          
        print()
    playAgain = input('Would you like to play again (y or n)?')




Is This A Good Question/Topic? 0
  • +

Replies To: Random key from dictionary

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Random key from dictionary

Posted 21 November 2011 - 07:51 AM

Dictionaries are not ordered elements, meaning that your random.shuffle doesn't do anything for you.

You can call random.choice() on dictionary.keys() to get a random key, and then look the value up in the dictionary.
Was This Post Helpful? 0
  • +
  • -

#3 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: Random key from dictionary

Posted 21 November 2011 - 08:27 AM

hmm but without the random.shuffle how am i going to scramble the letters of the word?


as far as my understanding goes i turn a random key from the dictionary into a list, then i shuffle the list so that the keys are out of order which will make you guess what the original key was, then loop back and do it all over again. the problem I am having is it keeps giving me the same random key from the dictionary.

could you elaborate a bit more motoma? I definitely want to understand.
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Random key from dictionary

Posted 21 November 2011 - 08:57 AM

I'm dreadfully sorry, I completely misread your program (not enough coffee, perhaps?).

Your issue is that you are only choosing a random word once. you will find that if you move your ranWord() call inside your while loop, your program will work as expected.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

Re: Random key from dictionary

Posted 21 November 2011 - 09:51 AM

Looks good. Your only real problem is that one nasty global. You need to call ranWord each time. Also, use more functions!

e.g.
import random

dictionary = { 
	'tuna': 'A fish',
	'spoon': ' eat soup with this', 
	'shoe' : ' wear these on your feet', 
	'jacket': ' put these on during winter',
	'book': ' read these', 
	'cup': ' drink out of these',
	'keyboard': 'use these to type',
	'watch': 'what time is it', 
	'processor': 'the central part of a computer',
	'key': 'unlock things with it', 
	'paper': 'organic writing surface', 
	'ninja': 'if seen it is too late for life', 
	'wallet': 'hold money in this', 
	'speaker': 'sound comes from these',
	'dinosaur': 'prehistoric badasses', 
	'dog': 'barks a lot', 
	'chair': 'makes sitting a pleasure', 
	'phone': 'allows you to instantly talk with people', 
	'sushi': 'a food from the gods discovered by the Japanese', 
	'printer': 'prints things', 
	'RAM': 'computer memory', 
	'wrench': 'a tool for tightening bolts', 
	'switch': 'turns things on or off', 
	'bicycle': '2 wheeled man powered vehicle', 
	}


def programIntro():
	print('I will show you a scrambled word and give you three chances to')
	print('guess the original word. After your second incorrect guess, I will help')
	print('by providing a hint.')
	print()
	print('Here we go ....')


def ranWord():
	return random.choice(list(dictionary.keys()))


def scramble(randomWord):
	daList = list(randomWord)
	random.shuffle(daList)
	print (''.join(daList))

	
def play():
	programIntro()
	print ('The scrambled word is... ')
	randomWord = ranWord() # this was the problem.  Always call a new one
	scramble(randomWord)
	loop = 0
	while loop < 3:
		loop = loop +1
		guess = input('what is your guess for guess number ' + str(loop) + ': ' )
		print()
		if guess == randomWord:
			print ('Good work, your guess is correct')
			break
		# you don't need this... think about it
		# if guess != randomWord:
		print ('Sorry, that is not correct')
		if loop == 2:
			print("Here's a hint ----->> " + (dictionary.get(randomWord,[])) ) 
		print()


def main():
	playAgain = 'y'
	while playAgain =='y':
		play()
		playAgain = input('Would you like to play again (y or n)?')

main()



I'd probably just return both on the ranWord, though. Save another lookup and avoids that global dictionary var. In fact, then you wouldn't need a dictionary at all.
e.g.
import random

def programIntro():
	print('I will show you a scrambled word and give you three chances to')
	print('guess the original word. After your second incorrect guess, I will help')
	print('by providing a hint.')
	print()
	print('Here we go ....')

def getRandomEntry():
	items = [('bicycle', '2 wheeled man powered vehicle'), ('sushi', 'a food from the gods discovered by the Japanese'), ('wallet', 'hold money in this'), ('paper', 'organic writing surface'), ('shoe', ' wear these on your feet'), ('keyboard', 'use these to type'), ('chair', 'makes sitting a pleasure'), ('cup', ' drink out of these'), ('RAM', 'computer memory'), ('book', ' read these'), ('speaker', 'sound comes from these'), ('printer', 'prints things'), ('watch', 'what time is it'), ('phone', 'allows you to instantly talk with people'), ('spoon', ' eat soup with this'), ('key', 'unlock things with it'), ('ninja', 'if seen it is too late for life'), ('tuna', 'A fish'), ('dog', 'barks a lot'), ('jacket', ' put these on during winter'), ('dinosaur', 'prehistoric badasses'), ('switch', 'turns things on or off'), ('wrench', 'a tool for tightening bolts'), ('processor', 'the central part of a computer')]
	return random.choice(items)

def scramble(randomWord):
	daList = list(randomWord)
	random.shuffle(daList)
	print ('The scrambled word is... ' + ''.join(daList))
	
def play():
	(word, desc) = getRandomEntry()
	programIntro()
	scramble(word)
	for loop in range(3):
		if word == input('what is your guess for guess number ' + str(loop+1) + ': ' ):
			print ('Good work, your guess is correct')
			return True
		print ('Sorry, that is not correct')
		if loop == 2:
			print("Here's a hint ----->> " + desc ) 
		print()
	print ('Sorry, the word was ' + word)
	return False

def askPlayAgain():
	return input('Would you like to play again (y or n)?')=='y'

def main():
	while True:
		play()
		if not askPlayAgain():
			break

main()



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1