无法捕获 IndexError 并打印语句,也循环回输入
Couldn't catch the IndexError and print the statement, also loop back to the input
我可以循环 ValueError,但只有 ValueError 会打印语句
并且 IndexError 将停止代码,我想继续向用户询问 'line number'。
def file_content(file_name):
user_file = open(file_name, 'r')
content = user_file.readlines()
user_file.close()
return content
def main():
file_name = input('Enter the name of the file: ')
try:
content = file_content(file_name)
except IOError:
print ('File can not be fount. Program will exit.')
exit()
while True:
try:
line_number = input('Enter a line number: ')
line_number = int(line_number)
break
except IndexError:
print ('That is not a valid line number. Try again.')
except ValueError:
print ('You need to enter an integer for the line number. Try again.')
print ('The line you requested:')
print (content[line_number-1])
main()
这就是我 运行 代码
时发生的情况
Enter the name of the file: datas.txt
Enter a line number: w
You need to enter an integer for the line number. Try again.
Enter a line number: 99
The line you requested:
Traceback (most recent call last):
File "/Users/yenvivian/Desktop/Python HW/hw04.py", line 40, in <module>
main()
File "/Users/yenvivian/Desktop/Python HW/hw04.py", line 37, in main
print (content[line_number-1])
IndexError: list index out of range
谢谢
要捕获异常,引发异常的代码必须在try
:
的内部
try:
line_number = input('Enter a line number: ')
line_number = int(line_number)
print ('The line you requested:')
print (content[line_number-1]) # Inside the try
break
except IndexError:
print ('That is not a valid line number. Try again.')
except ValueError:
print ('You need to enter an integer for the line number. Try again.')
我可以循环 ValueError,但只有 ValueError 会打印语句
并且 IndexError 将停止代码,我想继续向用户询问 'line number'。
def file_content(file_name):
user_file = open(file_name, 'r')
content = user_file.readlines()
user_file.close()
return content
def main():
file_name = input('Enter the name of the file: ')
try:
content = file_content(file_name)
except IOError:
print ('File can not be fount. Program will exit.')
exit()
while True:
try:
line_number = input('Enter a line number: ')
line_number = int(line_number)
break
except IndexError:
print ('That is not a valid line number. Try again.')
except ValueError:
print ('You need to enter an integer for the line number. Try again.')
print ('The line you requested:')
print (content[line_number-1])
main()
这就是我 运行 代码
时发生的情况Enter the name of the file: datas.txt
Enter a line number: w
You need to enter an integer for the line number. Try again.
Enter a line number: 99
The line you requested:
Traceback (most recent call last):
File "/Users/yenvivian/Desktop/Python HW/hw04.py", line 40, in <module>
main()
File "/Users/yenvivian/Desktop/Python HW/hw04.py", line 37, in main
print (content[line_number-1])
IndexError: list index out of range
谢谢
要捕获异常,引发异常的代码必须在try
:
try:
line_number = input('Enter a line number: ')
line_number = int(line_number)
print ('The line you requested:')
print (content[line_number-1]) # Inside the try
break
except IndexError:
print ('That is not a valid line number. Try again.')
except ValueError:
print ('You need to enter an integer for the line number. Try again.')