Skip to main content
 首页 » 编程设计

python之错误 : Invalid Syntax on "def"

2025年05月04日10hnrainll

所以我得到了 Invalid Syntax,它在 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 

请您参考如下方法:

您在 view_log 中的打印语句有一个额外的 [ 应该是

    print line[date[month]],line[date[day]],line[date[year]],line[number]