所以我得到语法无效,它在add_entry函数之前说def。我不知道为什么。我试着评论出来,然后在不同的功能上出现了同样的错误。我正在使用Python 2.7。
date,number = 0,1
month,day,year = 1,2,0
from datetime import datetime
def home():
option = ''
option = raw_input('Press ENTER to view log or input anything to add entries: ')
print '\n'
if option == '':
view_log()
else:
add_entry()
def view_log():
log_a = open('storage.txt', 'r')
log_b = log_a.read()
for line in log_b:
print line[date[month]],line[date[day]],line[date[[year]],line[number]
def add_entry():
old_entry = open('storage.txt', 'r')
save = ''
for line in old_entry:
save = save + line
new_entry = open('storage.txt','w')
new = input_entry()
save = save + str(new) + '\n'
new_entry.write(save)
def input_entry():
n_date = get_date()
print 'Todays date is: %s/%s/%s' %(n_date[month],n_date[day],n_date[year])
n_number = raw_input('What was todays number? ')
return (n_date,n_number)
def get_date():
time_a = datetime.now()
time_b = str(time_a)
time_c = time_b.split(' ')
time_d = time_c[0].split('-')
time_e = tuple(time_d)
return time_e
答案 0 :(得分:12)
view_log
中的打印声明有额外[
它应该是
print line[date[month]],line[date[day]],line[date[year]],line[number]