如何编写菜单驱动的python程序

如何编写菜单驱动的python程序,python,menu,Python,Menu,问题是: 编写一个菜单驱动的Python程序,包括以下内容: 从用户处获取文件名并创建同名文件的函数。然后,该函数应该使用while循环来处理该文件,并允许用户输入要写入该文件的若干整数 一种使用for循环读取文件内容并将其输出到屏幕的函数 一个函数,用于将多个整数附加到文件中 计算文件中包含的数字总数并打印 屏幕上的答案 以下是我迄今为止完成的代码,我只需要帮助完成它: def main(): filename = input("Welcom, please enter file

问题是:

编写一个菜单驱动的Python程序,包括以下内容:

  • 从用户处获取文件名并创建同名文件的函数。然后,该函数应该使用while循环来处理该文件,并允许用户输入要写入该文件的若干整数
  • 一种使用
    for
    循环读取文件内容并将其输出到屏幕的函数
  • 一个函数,用于将多个整数附加到文件中
  • 计算文件中包含的数字总数并打印 屏幕上的答案
以下是我迄今为止完成的代码,我只需要帮助完成它:

def main():
    filename = input("Welcom, please enter file name:\t")
    menu()
    choice= int(input("Enter menu choice:\t"))

    while choice != 5:
        #get file choice from user
        if choice == 1:
            #create file
            create(filename)
        elif choice == 2:
            #read file
            read(filename)
        elif choice == 3:
            #append file
            append(filename)
        elif choice == 4:
            #get total
            get_total(filename)

        choice = int(input("Enter menu choice:\t"))

    print("\nApplication Complete")

def menu():

    #user chooses a number from list
    print("Choose a number to continue:\t\n\
    Select 1 to create a file\n\
    Select 2 to read a file\n\
    Select 3 to append to a file\n\
    Select 4 to calculate the total of a file\n\
    Select 5 to exit programme")

def create(filename):

    #open file name
    outfile = open(filename,"w")
    again = "y"

    while again == "y":

        try:
            num = int(input("Input number:\t")
            outfile.write(str(num)+"\n")
            #asks user whether they want to continue or not
            again = input("Enter y to continue:\t")

        except ValueError:
                      print("An error occured,please enter an integer:\t")

        except:
                      print("An undetermined error occurred")
    #close file
    outfile.close()

def read(filename):

    read(filename)
    print("\nReading File)

    try:
        infile = open(filename,"r")

        for line in infile:

        number = int(line)
        print(number)

    except IOError:
            print("An error occured trying to read")
            print("the file", filename)

    except:
            print("An undefined error occurred")

def append(filename):

    append(filename)

    print("\nAppending to file")

    try:
        #create file object
        outfile = open(filename, "a")
        again = "y"

    while again == "y":

        try:
            num = int(input("input number to append to file:\t"))
            outfile.write(str(num)+"\n")
            again = input ("enter y to continue:\t")

        except ValueError:
                print("an error occured please an integer")
        except:
                print("an undefined error occured")

    except IOError:
            print("an error occurred trying to read")
            print("the file", filename)

    except:
            print("an undefined error occurred")

            infile.close()



#call main
main()
它一直在说,使用
outfile.write(str(num)+“\n”)
create
函数中出现了无效语法。另外,有人能帮我写一个函数,计算文件中包含的数字的总数,并将答案打印到屏幕上吗?我正在使用python 3.3.2


好的,我让代码正常工作了,我只需要帮助编写一个函数来计算文件中包含的数字的总数,并将答案打印到屏幕上,如果有人想帮忙的话,请

问题是上面一行的一个小错误:

num = int(input("Input number:\t")
两个
,但只有一个
,所以Python认为这个表达式继续到下一行,当然是对
outfile的调用。write
不能像那样直接跟随对
input
的调用,所以它是一个
语法错误

这是每个人在Python体验的早期都必须学习一次的内容:当您在一行中遇到一个不可理解的
语法错误时,请查看上面的一行,看看是否缺少
]


同时,您可能需要考虑使用一个与您匹配的更聪明的编辑器。

您丢失了<>代码> 在上面的行中。“还有,有人可以帮助我编写一个函数,计算文件中包含的数字的总数,并将答案打印到屏幕上吗?”在一个完全不同的问题中间没有人能做到这一点。发布一个新的问题,并尝试让它成为一个有具体答案的具体问题,而不是一个握手的请求。(点击“帮助”链接以获得有关编写好问题的帮助。)如果你阅读了问题的完整部分,我请求帮助完成代码,这是我无法完成的代码的一部分