【问题标题】:Why do none of the function calls execute like they're supposed to within this program?为什么没有一个函数调用像在这个程序中那样执行?
【发布时间】:2019-03-07 01:59:48
【问题描述】:

标题几乎说明了一切。每个小功能都可以独立工作(我在单独的文件中测试了它们),但是在放入主功能时它们不起作用。我究竟做错了什么? (代码块很长,对此感到抱歉) 基本问题是它在选择选项时它不会移动到执行功能,它只是结束。

    from sys import exit

def readFrom():
    file = input('What file do you want to read from? (Please include extension)')
    try:
        infile = open(file, 'r')

        line = infile.readline()
        cleanLine = line.strip()

        while line != '':
            print(cleanLine)
            line = infile.readline()
            cleanLine = line.strip()
        infile.close()
        menu()
    except IOError:
        print('Sorry, that is not a valid file. It might not exist or might not be in the correct directory.')

def writeTest():
    file = input('What file do you want to write to?')
    outfile = open(file,'w')

    addition = input('What would you like to write to the file?')
    try:
        addition = int(addition)
    except:
        print('Sorry, that is invalid.')
        writeTest()
    outfile.write(str(addition))
    outfile.close()
    menu()

def fileAppend():
    outfile = open('example.txt','a')

    addition = input('What would you like to add to the file?')
    try:
        addition = int(addition)
    except:
        print('Sorry, that is invalid.')
        fileAppend()
    outfile.write(str(addition))
    outfile.close()
    menu()

def menu():
    print('Hello! Welcome to the file editing helper!')
    answer = input('Selection Menu:\n'
                '0. Exit\n'
                '1. Read from file\n'
                '2. Write integers to a file\n'
                '3. Append integers to a file\n'
                'Which would you like to do? ')

    if answer == 1:
        readFrom()
    if answer == 2:
        writeTest()
    if answer == 3:
        fileAppend()
    if answer == 0:
        exit()

menu()

我已经尝试了所有我能想到的方法,我已经将完整的函数放入了我本来要调用函数的部分中,如果我不必按照要求将它们放在函数中我已经得到了,我不会在函数中使用它们。任何提示都是有帮助的,因为我真的不擅长这个。

【问题讨论】:

  • 它在我的机器上使用 python3 编译得很好。您在第一行有一个有问题的缩进,您的代码中的缩进方式是否完全相同?

标签: python function


【解决方案1】:

好的,在你的 menu() 中你要求输入,然后你将该输入与一个整数进行比较,但你的输入被认为是一个字符串。

示例:

user_input = input('Enter a number') # 7

user_input 将是 '7',而不是 7,所以你不能说

if user_input == 7:
    #do this

因为它永远不会。所以这里的菜单是固定的。

def menu():
    print('Hello! Welcome to the file editing helper!')
    answer = int(input('Selection Menu:\n'
                '0. Exit\n'
                '1. Read from file\n'
                '2. Write integers to a file\n'
                '3. Append integers to a file\n'
                'Which would you like to do? '))

    if answer == 1:
        readFrom()
    if answer == 2:
        writeTest()
    if answer == 3:
        fileAppend()
    if answer == 0:
        exit()

menu()

我将输入函数包装在一个 int() 方法中,现在应该可以使用了。 1 不等于 '1',这是您要执行的操作。

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多