此代码遇到问题:
return render(request, 'count.html',{'fulltext':fulltext,'count':len(wordlist),'worddictionary':worddictionary.items("")})
CMD告诉我第22行的语法无效,但是我找不到任何错误。
from django.http import HttpResponse
from django.shortcuts import render
import operator
def homepage(request):
return render(request, 'home.html')
def count(request):
fulltext = request.GET['fulltext']
wordlist = fulltext.split()
worddictionary = {}
for word in wordlist:
if word in worddictionary:
#increase
worddictionary[word] += 1
else:
#add to the dictionary
worddictionary[word] = 1
sortedwords = sorted(worddictionary.items(), key=operator.itemgetter(1), reverse=True)
返回render(request,'count.html',{'fulltext':fulltext,'count':len(wordlist),'worddictionary':worddictionary.items()})
答案 0 :(得分:1)
检查代码后,我看到您正在提供字典的键值对,例如:'count':len(word list)。我建议您假设x = len(word list)创建一个新变量,然后在字典中使用该变量,例如'count':x。我希望以后能解决。
答案 1 :(得分:0)
这可能是因为return语句末尾有一个额外的'。