2013-05-03 63 views
-3

所以我有了这个程序,它基本上是在加密许多不同的方式全文(考试的做法),我不知道如何使菜单功能藏汉接受大写字母为小写字母Python 2.7版接受大写字母

def GetMenuChoice(): 
    MenuChoice = raw_input() 
    print 
    return MenuChoice 

回答

2

二者必选其一str.lower()str.upper()你的文本转换为全部小写或全部大写:

def GetMenuChoice(): 
    MenuChoice = raw_input("Enter your choice: ") 
    return MenuChoice.lower() 

expected="Menu1" 
while GetMenuChoice() != expected.lower(): 
    print "Try again" 
print "Correct input"  

演示:

Enter your choice: menu 
Try again 
Enter your choice: mennu 
Try again 
Enter your choice: mEnU1 
Correct input 
+0

谢谢,我现在拥有它:) – user1655562 2013-05-03 08:52:32

0

如果你想写终端应用程序,我强烈建议使用curses。但作为一种快速入侵,只需将其转换为较小或大写,然后再进行检查。

0
>>> MenuChoice = raw_input() 
A 
>>> MenuChoice = MenuChoice.lower() 
>>> MenuChoice 
'a' 

这是你总可以应付一个情况下,在用户输入上调用.lower.upper