By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

While statement returns false even if it's true

P: 3
Even when the jumble is solved correctly, the program prints the try again message. What am I missing.


Expand|Select|Wrap|Line Numbers
  1.  
  2. # Led Zeppelin Word Jumble
  3. # to utilize loops, strings and tupules
  4. # Nathan Porter
  5. # 10/03/2011
  6.  
  7. import random
  8.  
  9. # create a sequence of words to choose from
  10. words = ("Robert Plant", "Jimmy Page", "John Bonham", "John Paul Jones",)
  11. hints = {"Robert" : "The greatest voice in rock",
  12.                "Jimmy" : "Yardbird",
  13.                "Bonham" : "R.I.P.  25 September 1980",
  14.                "Jones" : "triquetra"}
  15. word = random.choice(words)
  16. hint = 'hint'
  17. correct = word
  18. word1 = word
  19. count = 1
  20.  
  21.  
  22. # create a jumbled version of the word
  23. jumble =""
  24.  
  25. while word:
  26.     position = random.randrange(len(word))
  27.     jumble += word[position]
  28.     word = word[:position] + word[(position + 1):]
  29.  
  30.  
  31. score = 0
  32.  
  33. # start the game
  34. print(
  35. """
  36.            Welcome to Led Zeppelin Word Jumble!
  37.  
  38.    Unscramble the letters to make a word.
  39.    Type 'hint' for help or just press Enter to give up
  40.  
  41. """
  42. )
  43. print("\nThe jumble is:", jumble)
  44.  
  45. guess = input("Your guess:\n>>>")
  46. guess = guess.lower()
  47. if guess == "":
  48.     print ("You Lose!"),
  49. elif guess == hint:
  50.         if word1 == 'Robert Plant':
  51.             print (hints,("Robert"))
  52.         elif word1 == 'Jimmy Page':
  53.             print (hints,("Jimmy"))
  54.         elif word1 == 'John Bonham':
  55.             print (hints,("Bonham"))
  56.         elif word1 == 'John Paul Jones':
  57.             print (hints,("Jones"))
  58. while (guess != correct) and (guess != ""):
  59.     print ("Lots of people talk but few of them know...")
  60.     guess = input("Your guess:\n>>>")
  61.     guess = guess.lower()
  62.  
  63.     if guess == correct:
  64.         print ("...and it only goes to show, you know!\n"),
  65.  
  66.  
  67.  
  68. print("Thanks for playing") 
  69.  
  70. input("\n\nPress the enter key to exit.")
  71.  
Oct 13 '11 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,383
Odd coding choices aside, you convert the guess to lower case letters while your words are not.
Oct 13 '11 #2

P: 3
I'm teaching myself out of this book and using this forum. I'm having a hard time with it. What choices are odd?
Oct 13 '11 #3

P: 3
thank you for your help, I finally got it to work correctly, there was a problem with my elif statements as well
Oct 13 '11 #4

Rabbit
Expert Mod 10K+
P: 12,383
Some of the odd choices are that you have 3 variables that store the word choice. At most you need 2. Also, you have a variable count and score that you never use.

Some more advanced stuff is that you can combine the words dictionary with the hint dictionary. You can also use the key to get the hint rather than using that if elif structure.

But none of that stuff actually affects whether or not the code works. Glad you got it fixed, good luck.
Oct 13 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.