473,786 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if, elif, indentation, posting guidlines [solved]

2 New Member
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(W ORDS)
# 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.randrang e(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("\nYo ur 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 "complicate d"
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\n Press the enter key to exit.")
Oct 5 '06 #1
3 1963
bartonc
6,596 Recognized Expert Expert
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
eric dexter
46 New Member
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(W ORDS)
# 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.randrang e(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("\nYo ur 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 "complicate d"
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\n Press 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 New Member
(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
7097
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 programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
44
2120
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 would be sufficient. Many people think Python's syntax makes sense. There are strong feelings both ways. It must depend on a person's way of thinking, because I find it very confusing, even after using with Python for some time, and trying to...
5
1188
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 code with the indentation gone. Python code is fragile in this regard. One solution that occurs to me is that a "indentation code" could be appended to a script which could be used to rebuild the script if necessary. e.g. if we let letters...
7
5878
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 because indentation was not right. I checked time and again but still no success. I rewrote the code over again in VI and it ran. Can you please explain whats the trick behind the correct indentation. Thanks
13
1930
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 up? OLD_INDENT = 5 # spaces NEW_INDENT = 4 # spaces print 'Reindent.py:'
2
2599
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, Free/Open/NetBSD, Mac OS X, AT&T SysV r4, SCO Unix, AIX, OSF, NextStep. I want to add some conditionals like this: #if defined(USECODE_A) /*use this code */
2
4100
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 statement. I am going to post the first 82 lines of the script, since the error message points at line 80: from abaqusConstants import * from abaqus import *
2
1773
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 to attach to, the following code determines the X and Y direction flags to calculate the exact location and the rotation tuple of the bent plate object. Here's the old code (about 5 years old):if _relrotation > 0.0 and _relrotation < 90.0: ...
19
2106
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 year.... another initiative is exporting the speech recognition environment to the Linux context. In a nutshell, he dictated to application on Windows, it tunnels over the network to a Linux machine, and will allow you to cut and paste to and...
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.