While statement returns false even if it's true

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Porter
    New Member
    • Oct 2011
    • 3

    While statement returns false even if it's true

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


    Code:
    # Led Zeppelin Word Jumble
    # to utilize loops, strings and tupules
    # Nathan Porter
    # 10/03/2011
     
    import random
     
    # create a sequence of words to choose from
    words = ("Robert Plant", "Jimmy Page", "John Bonham", "John Paul Jones",)
    hints = {"Robert" : "The greatest voice in rock",
                   "Jimmy" : "Yardbird",
                   "Bonham" : "R.I.P.  25 September 1980",
                   "Jones" : "triquetra"}
    word = random.choice(words)
    hint = 'hint'
    correct = word
    word1 = word
    count = 1
     
     
    # create a jumbled version of the word
    jumble =""
     
    while word:
        position = random.randrange(len(word))
        jumble += word[position]
        word = word[:position] + word[(position + 1):]
     
    
    score = 0
     
    # start the game
    print(
    """
               Welcome to Led Zeppelin Word Jumble!
            
       Unscramble the letters to make a word.
       Type 'hint' for help or just press Enter to give up
    
    """
    )
    print("\nThe jumble is:", jumble)
    
    guess = input("Your guess:\n>>>")
    guess = guess.lower()
    if guess == "":
        print ("You Lose!"),
    elif guess == hint:
            if word1 == 'Robert Plant':
                print (hints,("Robert"))
            elif word1 == 'Jimmy Page':
                print (hints,("Jimmy"))
            elif word1 == 'John Bonham':
                print (hints,("Bonham"))
            elif word1 == 'John Paul Jones':
                print (hints,("Jones"))
    while (guess != correct) and (guess != ""):
        print ("Lots of people talk but few of them know...")
        guess = input("Your guess:\n>>>")
        guess = guess.lower()
     
        if guess == correct:
            print ("...and it only goes to show, you know!\n"),
      
    
     
    print("Thanks for playing") 
     
    input("\n\nPress the enter key to exit.")
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Odd coding choices aside, you convert the guess to lower case letters while your words are not.

    Comment

    • Nathan Porter
      New Member
      • Oct 2011
      • 3

      #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?

      Comment

      • Nathan Porter
        New Member
        • Oct 2011
        • 3

        #4
        thank you for your help, I finally got it to work correctly, there was a problem with my elif statements as well

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          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.

          Comment

          Working...