質問

((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.))

役に立ちましたか?

解決

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

他のヒント

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?")

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']
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top