468,293 Members | 1,858 Online

# if, elif, indentation, posting guidlines [solved]

Hi All,
I am new in learning programming. I am very lost. I read all of the chapters over and over again and I am still lost. I have the program below that I need to modify it to add hint to it when the user couldn't figure out the jumble word. Somehow the program didn't run. Can someone please look at it and enlighten me to see what I am missing? Is there any advise in learning and improving my programming skill? I am very disappointed at myself right now. It seems like I am from earth and programming language is from venus. I don't understand anything. Please help.

# Word Jumble
#
# The computer picks a random word and then "jumbles" it
# The player has to guess the original word

import random

# create a sequence of words to choose from
WORDS =("python",
"jumble",
"easy",
"difficult",
"xylophone")

# pick one word randomly from the sequence
word = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word

# create a jumbled version of the word
jumble =""
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 = guess.lower()
hint = "hint"
while (guess != correct) and (guess != "")and (guess =="hint"):
print "Sorry, that's not it. Would you like a hint?"
guess = guess.lower()
if guess ==hint and word =="easy":
print "not hard but"
guess = guess.lower()

elif guess ==hint and word =="python":
print "snake"
guess = guess.lower()

elif guess ==hint and word =="jumble":
print "shuffle"
guess = guess.lower()

elif guess ==hint and word =="difficult":
print "complicated"
guess = guess.lower()

elif guess ==hint and word =="answer":
print "solution"
guess = guess.lower()

else:
print "Musical instrument that consists of bars of various lengths."
guess = guess.lower()

if guess == correct:
print "That's it! You guessed it!\n"

print "Thanks for playing."

raw_input("\n\nPress the enter key to exit.")
Oct 5 '06 #1
3 1823
bartonc
6,596 Expert 4TB
Will you post this again? Only this time, look to the right of the message box and read the posting guidelines - the item that say "Use CODE tags" so that the structure of your code comes through. This way it's easy to copy and test your post.
Oct 5 '06 #2
Hi All,
I am new in learning programming. I am very lost. I read all of the chapters over and over again and I am still lost. I have the program below that I need to modify it to add hint to it when the user couldn't figure out the jumble word. Somehow the program didn't run. Can someone please look at it and enlighten me to see what I am missing? Is there any advise in learning and improving my programming skill? I am very disappointed at myself right now. It seems like I am from earth and programming language is from venus. I don't understand anything. Please help.

# Word Jumble
#
# The computer picks a random word and then "jumbles" it
# The player has to guess the original word

import random

# create a sequence of words to choose from
WORDS =("python",
"jumble",
"easy",
"difficult",
"xylophone")

# pick one word randomly from the sequence
word = random.choice(WORDS)
# create a variable to use later to see if the guess is correct
correct = word

# create a jumbled version of the word
jumble =""
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 = guess.lower()
hint = "hint"
while (guess != correct) and (guess != "")and (guess =="hint"):
print "Sorry, that's not it. Would you like a hint?"
guess = guess.lower()
if guess ==hint and word =="easy":
print "not hard but"
guess = guess.lower()

elif guess ==hint and word =="python":
print "snake"
guess = guess.lower()

elif guess ==hint and word =="jumble":
print "shuffle"
guess = guess.lower()

elif guess ==hint and word =="difficult":
print "complicated"
guess = guess.lower()

elif guess ==hint and word =="answer":
print "solution"
guess = guess.lower()

else:
print "Musical instrument that consists of bars of various lengths."
guess = guess.lower()

if guess == correct:
print "That's it! You guessed it!\n"

print "Thanks for playing."

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

I noticed an odd char in one line maybe a char missing or some such with a smiley face. don't know how much that helps though

word = word[osition] + word[(position + 1):]
Oct 6 '06 #3
(The odd character is just the two characters :p being interpreted as the :p emoticon because the code wasn't posted inside of code tags.)

The if-elif chain in your main loop is indented too far. You probably want something like
Expand|Select|Wrap|Line Numbers
1. while ( (guess != correct) and (guess != "") ) or (guess =="hint"):
2.     print "Sorry, that's not it. Would you like a hint?"
3.     guess = raw_input("Your guess: ")
4.     guess = guess.lower()
5.     if guess ==hint and word =="easy":
6.         print "not hard but"
7.         guess = raw_input("Your guess: ")
8.         guess = guess.lower()
9.
10.     elif guess ==hint and word =="python":
11.         print "snake"
12.         guess = raw_input("Your guess: ")
13.         guess = guess.lower()
14.
15.     elif guess ==hint and word =="jumble":
16.         print "shuffle"
17.         guess = raw_input("Your guess: ")
18.         guess = guess.lower()
19.
20.     elif guess ==hint and word =="difficult":
21.         print "complicated"
22.         guess = raw_input("Your guess: ")
23.         guess = guess.lower()
24.
25.     elif guess ==hint and word =="answer":
26.         print "solution"
27.         guess = raw_input("Your guess: ")
28.         guess = guess.lower()
29.
30.     else:
31.         print "Musical instrument that consists of bars of various lengths."
32.         guess = raw_input("Your guess: ")
33.         guess = guess.lower()
(Also, the test in the while loop should probably have an or in front of the hint check, instead of an and.
Oct 10 '06 #4

### This discussion thread is closed

Replies have been disabled for this discussion.