Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Y or N in input

So I recently saw a thread on hackforums asking how to ask only for a 'y' or 'n' input from the user and how to make it keep asking the person that question until the right input has been reached. Since it was still an open-ended question, I decided to answer it.

The thread I am talking about is here:
http(://)hackforums(.)net/showthread.php?tid=5042581

PHP Code

<?php 
while True:
    
play = raw_input("Ready to play? (Y/N): ").lower()
    if 
play != "y" and play != "n":
        print (
"You have not selected Y or N. Please select a valid option.")

    if 
play == "y":
        print (
"Let the game's begin")
        break 
#replace break code with thing you want to actually execute
    
elif play == "n":
        print (
"Ok you don't want to play")
        break 

Credits:
Evolution - help with appending .lower() for more universal input

Hope Aged finds this thread useful 🙂
I'll pm him the link to this thread if he still needs it
[Image: AO23sFn.png]
(Nov 7, 2015 02:06 PM)SavageAF Wrote:  I'll pm him the link to this thread if he still needs it

Thank you 👍
Change the Y/Ns in the ifs to lowercase and append .lower() to the raw_input line to avoid case issues.
(Nov 7, 2015 02:19 PM)SavageBoys Wrote:  Change the Y/Ns in the ifs to lowercase and append .lower() to the raw_input line to avoid case issues.

I thought of doing that too, but wanted to ask Aged first if he wanted it that way. I'll edit OP to what you mentioned and put you in credits
I had already PM'd him the full code.
[Image: npafFhp.gif]
I ended up using
PHP Code

<?php 
play 
= input("Ready to play? (Y/N): ")
while 
play != "Y" and play != "N":
    print (
"You have not selected Y or N, please enter a valid option")
    
play = input("Ready to play? (Y/N): ") 
thanks for the help though :) Also, is raw_input still used in 3.5?
(Nov 8, 2015 09:22 AM)Ruby On Rails Wrote:  I ended up using
PHP Code

<?php 
play 
= input("Ready to play? (Y/N): ")
while 
play != "Y" and play != "N":
    print (
"You have not selected Y or N, please enter a valid option")
    
play = input("Ready to play? (Y/N): ") 
thanks for the help though :) Also, is raw_input still used in 3.5?


Sorry, it should be just input(). I coded this back in Python 2.7; I'm a dinosaur I know lol
(Nov 8, 2015 10:24 AM)Baba Yaga Wrote:  Sorry, it should be just input(). I coded this back in Python 2.7; I'm a dinosaur I know lol

Don't let the version numbers fool you, think of everything above 2.7 as sort of the development branch.

2.7 is very much still the primary with 2.7.10 having been released last May.
Theres also no reason to have if-elif condition be checked if input != 'y' or 'n'
If you're going to do anything, do it right.
Psssst (Click to View)
HF XMPP: [email protected]

Click here for full specs.
[Image: Yaur5o6.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Un-IdentifiedJan 30, 2014 07:52 AM 4 141 Jan 30, 2014 07:52 AM
Last Post: JoinSeventh