#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    3

    While loop asking for two inputs


    So I am trying to re write a jumble word program to add hints and also reward players for not viewing hints.

    I have been trying to make it to where the only inputs accepted when the program asks if you want a hint is "Y" or "N". I am using a nested while loop and the program repeats the question if they want a hint twice. I am pretty sure its how I have it set-up but I am not sure how to make it ask only once. Here is the code.

    Code:
    # Word Jumble
    # The computer picks a random word and then "jumbles" it
    # The player has to guess the original word
    
    import random
    
    WORDS = ("python",
             "jumble",
             "easy",
             "difficult",
             "answer",
             "xylophone")
    HINTS = ("\nDynamic programming language.",
             "\nA way of mixing things up.",
             "\nVery simple.",
             "\nNot very simple.",
             "\nNeeded after a question.",
             "\nA weird word starting with X.")
    word = random.choice(WORDS)
    correct = word
    jumble = ""
    
    while word:
        position = random.randrange(len(word))
        jumble += word[position]
        word = word[:position] + word[(position + 1):]
    
    print("""
    
            Welcome to Word Jumble!
    
     Unscramble the letters to make a word.
    (Press the ENTER key at prompt to quit.)
    
    """)
    
    print("The jumble is:",jumble)
    
    guess = input("\nYour guess: ")
    while guess != correct and guess != "":
        print("\nSorry that's not it.")
        hint = input("\nWould you like a hint?  Y/N: ")
        hint_views = 1
        while hint != "Y" or "N":
            print("\nPlease choose either 'Y' or 'N'.")
            hint = input("\nWould you like a hint?  Y/N: ")
            if hint == "Y" and correct == WORDS[0]:
                print(HINTS[0])
                hint_views += 1
                break
            if hint == "Y" and correct == WORDS[1]:
                print(HINTS[1])
                hint_views += 1
                break
            if hint == "Y" and correct == WORDS[2]:
                print(HINTS[2])
                hint_views += 1
                break
            if hint == "Y" and correct == WORDS[3]:
                print(HINTS[3])
                hint_views += 1
                break
            if hint == "Y" and correct == WORDS[4]:
                print(HINTS[4])
                hint_views += 1
                break
            if hint == "Y" and correct == WORDS[5]:
                print(HINTS[5])
                hint_views += 1
                break
            if hint == "N":
                print("\nOK, maybe next time!")
                break
        guess = input("\nYour guess: ")
        if guess == correct and hint_views >= 2:
            print("\nThat's it!  Hmmm, you viewed the hints",hint_views,"times.")
            print("You are basically dumb, but congratulations!")
        if guess == correct and hint_views <= 1:
            print("\nThat's it!  Wow, your pretty smart.  You guess it without")
            print("looking at any hints!")
            
    input("\nPress the ENTER key to exit.")
    Everything works ok, just when the program asks if you want a hint, it asks twice.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    3
    while hint != "Y" or "N":

    The problem is with this while loop I think. It makes the program ask if the user wants a hints twice before it can move on to the if statements.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    While Python code can often resemble natural language, this is not one of those cases. Your condition will be interpreted as:
    Code:
    (hint != "Y") or "N"
    And since "N" is a non-empty string, it evaluates to true, so the while condition is always satisfied. You need to write it out in full:
    Code:
    hint != "Y" and hint != "N"
    Alternatively you can do it like this:
    Code:
    hint not in {"Y", "N"}
    (This may be nicer if you wanted to make some other inputs besides "Y" and "N" meaningful.)

    On an unrelated note, to make things a bit more user-friendly, consider converting input to uppercase before doing the comparison; that way a lowercase "y" or "n" can be used as well.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    39
    Rep Power
    3
    Thank you

IMN logo majestic logo threadwatch logo seochat tools logo