菜单提示不起作用

时间:2015-05-09 16:41:15

标签: python python-3.x

我用Python编写了一个应用程序来处理字符串,我做了一个菜单提示,我可以从中选择要执行的操作,我测试了所有的功能,它们运行良好,除了菜单和主要功能,当我输入我的选择没有任何反应这是代码:

import re
import os

def searchInFile(searched, file):
    try:
        f = open(file)
    except  IOError.strerror as e:
            print("Couldn't open the file: ", e)
    else:
        sea = re.compile(searched, re.IGNORECASE)
        for line in f:
            if re.search(sea, line):
                print(line, end='')

def validateWithPattern(string):
    patt = re.compile('([a-z0-9-_.])+@([a-z]+.)([a-z]{2,3})', re.IGNORECASE)
    if re.search(patt, string):
        print("Valid")
    else:
        print("Not valid!")

def menu():
    print("|================MENU=================|\n", end='')
    print("|0- Exit the program                  |\n", end='')
    print("|1- Validate mail address             |\n", end='')
    print("|2- Search a string in a file         |\n", end='')
    print("|=====================================|\n", end='')
    b = input("->")
    return b

def main():
    b = 10
    while b != 0:
        b = menu()
        if b == 1:
                a = input('Enter you mail address: ')
                validateWithPattern(a)
        elif b == 2:
                a = input('Enter the file name: ')
                c = input('Enter the string: ')
                searchInFile(c, a)
        elif b == 0:
                os.system("PAUSE")
    else: print("Choice error")

if __name__ == "__main__": 
     main()

1 个答案:

答案 0 :(得分:0)

b函数返回的menu变量是一个字符串,而不是整数,因此您无法将其与数字进行比较(输入会自动保存为字符串)。如果您使用return int(b)代替return b,则可以。

此外,我认为您的else: print("Choice error")应该与ifelif的缩进方式相同,因为您可能希望编写"选择错误"只有b不是给定选择之一。它在代码中缩进的方式将打印"选择错误"在while循环结束后,如果您输入无法识别的值(例如3),它将不会打印该消息。

相关问题