473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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",
"answer",
"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 = raw_input("\nYour guess: ")
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 = raw_input("Your guess: ")
guess = guess.lower()
if guess ==hint and word =="easy":
print "not hard but"
guess = raw_input("Your guess: ")
guess = guess.lower()

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

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

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

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

else:
print "Musical instrument that consists of bars of various lengths."
guess = raw_input("Your guess: ")
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 1949
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",
"answer",
"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 = raw_input("\nYour guess: ")
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 = raw_input("Your guess: ")
guess = guess.lower()
if guess ==hint and word =="easy":
print "not hard but"
guess = raw_input("Your guess: ")
guess = guess.lower()

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

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

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

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

else:
print "Musical instrument that consists of bars of various lengths."
guess = raw_input("Your guess: ")
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
brokow
7
(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
44
by: Joe | last post by:
Is Python going to support s syntax the does not use it's infamous whitespace rules? I recall reading that Python might include such a feature. Or, maybe just a brace-to-indentation preprocessor...
5
by: smichr | last post by:
I have (inadvertently) wiped out the functionality of my personal python snippets by eliminating leading space. I have also just visited http://www.python.org/tim_one/000419.html and saw a piece of...
7
by: diffuser78 | last post by:
I am a newbie to Python. I am mainly using Eric as the IDE for coding. Also, using VIM and gedit sometimes. I had this wierd problem of indentation. My code was 100% right but it wont run...
13
by: Jim | last post by:
Could somebody tell me why I need the "elif char == '\n'" in the following code? This is required in order the pick up lines with just spaces in them. Why doesn't the "else:" statement pick this...
2
by: Charles Sullivan | last post by:
I'm trying to maintain some older C code (FOSS) which has been patched by various individuals over the years for portability to multiple Unix-like operating systems, to wit: Linux, SunOS, Solaris,...
2
by: juan-manuel.behrendt | last post by:
Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else...
2
bvdet
by: bvdet | last post by:
We are parametrically attaching a bent plate object to the side of a building column for support of a skewed beam. Given a relative rotation between the column and beam and which side of the column...
19
by: Eric S. Johansson | last post by:
Almar Klein wrote: there's nothing like self interest to drive one's initiative. :-) 14 years with speech recognition and counting. I'm so looking to my 15th anniversary of being injured next...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.