Python 2.7接受大写字母

时间:2013-05-03 08:37:43

标签: python

所以我有这个程序,它基本上以多种不同的方式加密文本(用于考试练习),我不知道如何使菜单功能接受大写字母和小写字母

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果您想撰写终端应用,我强烈建议您使用curses。但作为一个快速的黑客,只需将其转换为小写或大写,然后检查它。

答案 2 :(得分:0)

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

通过在用户输入上调用.lower.upper,您可以随时处理一个案例。