【发布时间】: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 编译得很好。您在第一行有一个有问题的缩进,您的代码中的缩进方式是否完全相同?