我正在尝试构建一个函数,它要求用户输入一些字符(称这个数字为n),然后输入一个文件名。脚本应该打开文件,在屏幕上一次显示n个字符的内容,然后关闭文件。如果文件不存在,脚本应该反复要求用户输入不同的文件名。我的代码中似乎有一个bug:
myInput = input
print('please enter a positive integer: ')
myInput = n
try:
opened_file = open(filename)
chars = opened_file.read(n)
while chars != "":
chars = opened_file.read(n)
print(chars)
opened_file.close()
except IOError:
print('Please enter a different file name: ')
input()
顺便说一句,我不知道是什么错误,它说的都是语法错误。如果有人能帮忙,请帮忙。你知道吗
你宁愿使用
sys.stdin
而不是input
来让python处理字符串编码+许多小错误:我的答案实际上构建了一个函数,要求用户输入“许多字符”。。希望您喜欢;)
相关问题 更多 >
编程相关推荐