Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 26th, 2012, 04:30 PM
jomiscli jomiscli is offline
Registered User
Click here for more information
 
Join Date: Dec 2012
Posts: 29 jomiscli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 39 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old December 26th, 2012, 04:37 PM
jomiscli jomiscli is offline
Registered User
Click here for more information
 
Join Date: Dec 2012
Posts: 29 jomiscli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 39 sec
Reputation Power: 0
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.

Reply With Quote
  #3  
Old December 26th, 2012, 07:42 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 76 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 26 m 18 sec
Reputation Power: 2
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.

Reply With Quote
  #4  
Old December 27th, 2012, 02:25 AM
jomiscli jomiscli is offline
Registered User
Click here for more information
 
Join Date: Dec 2012
Posts: 29 jomiscli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 39 sec
Reputation Power: 0
Thank you

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > While loop asking for two inputs

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap