我试图写一个文件,将打开2个文本文件中的一个。我希望它弹出一个小问题,问你想要文件1或2,然后当我选择该文件时,它只是打开那个。我不认为它对我如何决定的选择感到满意。
1 = p.txt和2 = q.txt btw
def open_function (f):
print('which file would you like? type 1 for p and 2 for q')
choice = raw_input('> ')
if choice == 1:
file_choice=p.txt
elif choice == 2:
file_choice=q.txt
f=open('file_choice','r')
for i in range (1): first_line=f.readline()
rest=f.readlines()
f.close
我一直在
File "<ipython-input-10-383b19133bad>", line 6
elif choice == 2:
^
SyntaxError: invalid syntax
我该怎么做才能在2之间做出选择
你的缩进被打破了。在if
之后缩小线条如下:
if choice == 1:
file_choice = 'p.txt'
elif choice == 2:
file_choice = 'q.txt'
此外,您需要引用您的字符串,如图所示。
int
中指定raw_input
进行比较raw_input
def open_function (f):
#print('which file would you like? type 1 for p and 2 for q')
choice = int(raw_input('which file would you like? type 1 for p and 2 for q > '))
if choice == 1:
file_choice='p.txt'
elif choice == 2:
file_choice='q.txt'
f=open(file_choice,'r')
for i in range (1):
first_line=f.readline()
rest=f.readlines()
f.close