0

((In response to the above edit, this was not answered in the above link. The above question is irrelevant to my intended use.))

I have read a similar question about turning a string into lowercase;

How to convert string to lowercase in Python

I understand how this works perfectly, however my attempts at this myself have failed.

Here's my current setup example for a debug block;

#Debug block - Used to toggle the display of variable data throughout the game for debug purposes.
def debug():
    print("Would you like to play in Debug/Developer Mode?")
    while True:
        global egg
        choice = input()
        if choice == "yes":
            devdebug = 1
            break
        elif choice == "Yes":
            devdebug = 1
            break
        elif choice == "no":
            devdebug = 0
            break
        elif choice == "No":
            devdebug = 0
            break
        elif choice == "bunny":
            print("Easter is Here!")
            egg = 1
            break
        else:
            print("Yes or No?")
 #

So, I have it prewritten to work with a different capitalization. However, I'd like to use only one if statement per word rather than using two for the capitalization. I do have an idea, that uses another block to determine a True of False state, which would look like this;

def debugstate():
    while True:
        global devstate
        choice = input()
        if choice == "Yes":
            devstate = True
            break
        elif choice == "yes":
            devstate = True
            break
        elif choice == "No":
            devstate = False
            break
#Etc Etc Etc

But using this block would just take the lines of code I already have, and move it somewhere else. I know I could set it up that if it isn't 'Yes' then the else can automatically set devstate to 0, but I prefer to have a more controlled environment. I don't want to accidentally type 'yes ' with a space and have devmode off.

So back to the question;

How would I make it so I can just do the following?

def debug():
    print("Debug Mode?")
    while True:
        global egg
        choice = input()
        if choice == "yes" or "Yes":
            devdebug = 1
            break
        elif choice == "no" or "No":
            devdebug = 0
            break
        elif choice == "egg":
            devdebug = 0
            egg = 1
            print("Easter is Here")
            break
        else:
            print("Yes or No?")
#

The above code probably isn't the best example, but it at least helps me get my point across when I say I only want one if statement per word. (Also, I hope I didn't just solve my own problem here xD.)

So, how would I do this?

((Also, the reason I go here rather than the Python forums is because I prefer to ask my question in my own way rather than trying to piece together an answer from a question that was worded differently for someone else.))

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anthony Dragone
  • 63
  • 1
  • 2
  • 9
  • 2
    _"I understand how this works perfectly, however my attempts at this myself have failed"_. I would be interested in seeing the code where you tried to use `.lower`. – Kevin Apr 10 '14 at 13:55
  • 1
    just use if choice.lower() == "yes" and you're done – Retozi Apr 10 '14 at 13:56
  • @Kevin It was the placement of .lower that confused me. "How to convert string to lowercase in python" for me, didn't shout out to me where I would put .lower – Anthony Dragone Apr 10 '14 at 13:59
  • @Wooble I looked at that question and it did Not help me in anyway. – Anthony Dragone Apr 10 '14 at 14:09
  • @GameWylder You couldn't see that your `choice == "yes" or "Yes"` condition is effectively `(choice == "yes") or ("Yes")` - where the `"Yes"` part will _always_ evaluate as `True` - after reading a question with solutions explaining exactly why that's not the way to write what you wanted? – Matthew Trevor Apr 10 '14 at 14:23
  • @MatthewTrevor Im very particular about how I word questions. Also I understand the Boolean functions properly, the only thing I needed help with is formatting it properly. – Anthony Dragone Apr 10 '14 at 14:34
  • Your understanding of how conditions are evaluated isn't evident in your question. – Matthew Trevor Apr 10 '14 at 14:35

3 Answers3

8

Using .lower() is your best option

choice = input()
choice = choice.lower()
if choice == 'yes':
    dev_debug = 1
    break

Or use 'in'

choice = input()
if choice in ('yes', 'Yes'):
    dev_debug = 1
    break
R Hyde
  • 10,301
  • 1
  • 32
  • 28
  • I tried the first example, and it worked perfectly! And thanks for the second example, it's definitely something I'll use in the future. Thank you again or showing the proper setup. – Anthony Dragone Apr 10 '14 at 14:07
  • @GameWylder The second example is _exactly_ the same as the accepted solution to the question Wooble linked you to. – Matthew Trevor Apr 10 '14 at 14:25
2

You may put a lowercased value in some variable and use that instead of original choice where you don't care about case and you can still use choice where the case is significant:

def debug():
    print("Debug Mode?")
    while True:
        global egg
        choice = input()
        lowchoice = choice.lower()
        if lowchoice == "yes":
            devdebug = 1
            break
        elif lowchoice == "no":
            devdebug = 0
            break
        elif choice == "egg":
            devdebug = 0
            egg = 1
            print("Easter is Here")
            break
        else:
            print("Yes or No?")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jan Spurny
  • 5,219
  • 1
  • 33
  • 47
0

One word answer: string.lower()

To find out what variables/methods are available for a class/object, just use dir(object/class/method())

For e.g.

a="foo"
type(a)
<type 'str'>
dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Neerav
  • 1,399
  • 5
  • 15
  • 25