Jump to content

Python two challanges help!

- - - - -

  • Please log in to reply
13 replies to this topic

#1
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
I'm reading a book and i have a challange to do this:

Improve "Word Jumble" so that each word is paired with a hint. The player should be able to see the hint if he or she is stuck. Add a scoring system that rewards players who solve a jumble without asking for the hint.

Can anyone show me where i've gone wrong?



# Word Jumble

#

# The computer picks a random word and then "jumbles" it

# The player has to guess the original word

#

# Michael Dawson - 1/28/03

import random


# create a sequence of words to choose from

WORDS = ("python", "jumble", "easy", "difficult", "answer")


# pick one word randomly from the sequence

word = random.choice(WORDS)


#adding a scoring system

score = 100



# create a variable to use later to see if the guess is correct

correct = word


# create a jumbled version of the word

jumble =""



#adding scoring to hint:

hint = 0


while word:

    position = random.randrange(len(word))

    jumble += word[position]

    word = word[:position] + word[(position + 1):]


# start the game

print \

"""

Welcome to Word Jumble!

Unscramble the letters to make a word.

(Press the enter key at the prompt to quit.)

"""

print "The jumble is:", jumble


guess = raw_input("\nYour guess: ")

guess = guess.lower()


while (guess != correct) and (guess != ""):

    

    guess = raw_input("Your guess: ")

    guess = guess.lower()

    if guess == "hint" and word == "python":

        print "it's a programming language"

        score -=10

        hint += 1

    elif guess =="hint" and word == "jumble":

        print "It's what your playing now"

        score -=10

        hint += 1

    elif guess =="hint" and word =="easy":

        print "Opposite of hard is..."

        score -=10

        hint += 1

    elif guess =="hint" and word =="difficult":

        print "this word jumble is pretty d...."

        score -=10

        hint += 1

    elif guess =="hint" and word =="answer":

        print "the a....r"

        score -=10

        hint += 1

    elif guess == correct:

        score +=20

        print "thats it! You guessed it!\n"

        print "You got ", score, "points"

        if score == 100 and hint == 0:

            score +=100

            print "\nYou got an extra 100 points for not having any hints!"

            break

        


    


print "Thanks for playing."

raw_input("\n\nPress the enter key to exit.")



Also having troubles with this:

The challange is to:

Create a game where the computer picks a random word and the player has to guess that word. The computer tells the player how many letters are in the word. Then the player gets five chances to ask if a letter is in the word. The computer can only respond with "yes" or "no". Then, the player must guess the word.



I've had a go and it keeps having problems, like it'll say the word is correct even though it's not correct, which is very weird.

Any tips?


#computer guessing word program

#lamine haddadi

import random


WORDS =("bottle", "screen", "controller", "guitar")


chance = 1

count = 0


random = random.choice (WORDS)

print "There are ", len(random), "letters in the word"

print random


raw_input("Press enter to continue...")


print "\n\nYou have five chances to guess which letter are in the word"


letters = raw_input("what letter do you guess? ")


for i in range(4):

    if letters in random:

        print "yes"

        letters = raw_input("what letter do you guess?")

    else:

        print "no"

        letters = raw_input("what letter do you guess?")


print "Time to have a guess..."


guess = raw_input("What is the word? ")


if guess == random:

    print "Well done thats correct"

else:

    print "thats incorrect, try again"

    guess = raw_input("\nWhat is the word? ")


print "Well done thats correct"

        


raw_input("Press enter to continue...")


Thanks


thank you.

Edited by synk, 15 February 2011 - 11:09 AM.


#2
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
if guess == random:     print "Well done thats correct" else:     print "thats incorrect, try again"     guess = raw_input("\nWhat is the word? ")  print "Well done thats correct"

Based on the code above if the user enters the wrong word on the first try they will get a second chance to enter a new word. In this case however "Well done thats correct" will be printed whether or not the correct work is entered since there is no code to check the correctness the second time around. You will need to either add another if statement to check if the correct word is entered or you can just use a loop along with your old code if you are going to give more that two chances. I would use a loop either way.

#3
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts

solartic said:


if guess == random:     print "Well done thats correct" else:     print "thats incorrect, try again"     guess = raw_input("\nWhat is the word? ")  print "Well done thats correct"


Based on the code above if the user enters the wrong word on the first try they will get a second chance to enter a new word. In this case however "Well done thats correct" will be printed whether or not the correct work is entered since there is no code to check the correctness the second time around. You will need to either add another if statement to check if the correct word is entered or you can just use a loop along with your old code if you are going to give more that two chances. I would use a loop either way.

Silly mistake, thank you.

If anyone else was wondering, i just replaced it with this code:


while guess:

    if guess == random:

        print "Well done, thats correct."

        break

    else:

        print "Sorry thats incorrect, try again."

        guess = raw_input("What is the word? ")


Pretty simple.

If anyone could help on first challange it'd be great, thanks.

#4
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
I've edited the first challange, now if the user types hint, nothing comes up, the hint function is not working. Also, the scoring is not working :S. Help!

#5
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
I remember when I used to have this issue a lot lol. You modified the word variable in the loop and changed its value which is why your hint section is not working. I'm guessing this is just a simple mistake, and you know that all you need to do is make a copy of the variable before you modify it. I'm guessing that your scoring code will work when your hint code starts working.

#6
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Could you explain more about making a copy of the word variable, perhaps a example instead of the straight answer.

Thanks.

#7
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
Something like below:

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


#8
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Thanks, but how can this apply to adding hints? Sorry i'm fairly new :P

#9
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
That cool my explanation skills can be bad at times lol. So you added code to give a hint when the user typed "hint" but this new code was not working. The reason why your code was not working is because word would always == "" when it reach your hint section.

if guess == "hint" and word == "python": #word == "" at this and all other points below

#word == "python" at this point word has a correct value
while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
#word == "" the value of word has changed

So all my code did was prevent the value of word from changing so that it would have the right value when it reach your hint section. This was done by copying the value of word to another variable and using that variable in the for loop.

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

#word == "python"
#word_copy == ""



#10
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Thanks i'm starting to understand it more.

But how would i rewrite the word while loop? The one which gives the hint. I'm still pretty unsure with this, mainly because i'm not testing these examples as i'm not home; but i will in a few hours. However further explanations on how you make the word not have a empty value in the loop would be great :D. Also on how i can put this all together.

Thanks again for responding & sorry for the questions.

#11
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
word_copy = word
while word_copy:
    position = random.randrange(len(word_copy))
    jumble += word_copy[position]
    word_copy = word_copy[:position] + word_copy[(position + 1):]

You just need to change your code to something like this. Because you don't use word in the loop, you use word_copy instead, the value of word stays the same.

#12
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Where would i add the

word_copy = word

while word_copy:

    position = random.randrange(len(word_copy))

    jumble += word_copy[position]

    word_copy = word_copy[:position] + word_copy[(position + 1):]


In this loop? Or outside?


while (guess != correct) and (guess != ""):


Also, i don't see why the word value is staying empty. As i've stated that if guess == "hint" AND word == "python" wouldn't that make the word have a value?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users