擅长:python、mysql、java
<p>函数的作用是:返回一个包含行的列表,然后根据用户输入打印索引值。在</p>
<pre><code>file = input("Enter the file name: ")
text_file = open(file, "r")
lines = text_file.readlines()
print (len(lines))
while True:
linenumber = int(input("Please enter a line number or press 0 to quit: "))
if linenumber == 0:
print("Thanks for using the program")
break
elif 1 <= linenumber <= len(lines) :
print (lines[linenumber- 1])
else:
print("Please enter valid line number")
text_file.close()
</code></pre>