2017-10-11 3 views
1

ユーザーが小文字に入力するものを変更するコードがありますが、これを["a"または "A"]を使用する代わりに自分のコードにどのように実装すればよいでしょうか?ユーザー入力を小文字に変更するコード

optionchoice = input("Please enter A,B,C or D:").lower()

あなたはどんなユーザタイプの小文字バージョンへの入力を強制され、この方法:

def displaymenu(): 
    print("Weather station") 
    print("Please enter the option you would like") 
    optionchoice = input("Please enter A,B,C or D:") 

    if optionchoice in ["a" or "A"]: 
     print("The temperature will be displayed") 
     time.sleep(1) 
     optionA() 

    elif optionchoice in ["b" or "B"]: 
     print("The wind speed will be displayed") 
     time.sleep(1) 
     optionB() 

    elif optionchoice in ["c" or "C"]: 
     print("The day and time will be displayed") 
     time.sleep(1) 
     optionC() 

    elif optionchoice in ["d" or "D"]: 
     print("The Location will be displayed") 
     time.sleep(1) 
     optionD() 

    else: 
     print("Please type a valid input") 
     displaymenu() 
+3

あなたをそれを小文字にするために '.lower()'を使うことができます – GreenSaber

+0

私はこれを私のコードに入れてもらえませんか、全く分かりません –

+0

'optionchoice = input(" A、B、CまたはDを入力してください。 () ' –

答えて

1

はこのような何かを試してみてください。例えば

0

実際にPythonはメソッドに建てられた

string = "ABCDabcd123" 
lowercase = string.lower() 
print(lowercase) 

はあなたにabcdabcd123を与えます!

はそれを試してみて、ここではそれについての詳細を見つける: https://www.tutorialspoint.com/python/string_lower.htm

0

あなたのpython 2を使用する場合はraw_input().lower()を使用する必要があります。

0

str.lower()方法を使用して、あなたはそうのようなあなたのコードに変更を加えることができます:

def displaymenu(): 
    print("Weather station") 
    print("Please enter the option you would like") 

    optionchoice = input("Please enter A, B, C or D: ").lower() # Convert input to lowercase. 

    if optionchoice == 'a': 
     print("The temperature will be displayed") 
     time.sleep(1) 
     optionA() 

    elif optionchoice == 'b': 
     print("The wind speed will be displayed") 
     time.sleep(1) 
     optionB() 

    elif optionchoice == 'c': 
     print("The day and time will be displayed") 
     time.sleep(1) 
     optionC() 

    elif optionchoice == 'd': 
     print("The Location will be displayed") 
     time.sleep(1) 
     optionD() 

    else: 
     print("Please type a valid input") 
     displaymenu() 

あなたのバージョンに固執したい何らかの理由で、そのような入力検証する場合:

if optionchoice in ['a', 'A']: 
関連する問題