小编Cha*_*son的帖子

如何在 Python 中为 Hackerrank 解决闰年函数?

我一生都无法在 Hackerrank 上解决这个挑战。我得到的最接近的是 4/6 次传球。规则:在公历中,必须考虑三个标准来确定闰年:

The year can be evenly divided by 4, is a leap year, unless:
    The year can be evenly divided by 100, it is NOT a leap year, unless:
        The year is also evenly divisible by 400. Then it is a leap year.
Run Code Online (Sandbox Code Playgroud)

代码:

The year can be evenly divided by 4, is a leap year, unless:
    The year can be evenly divided by 100, it is NOT a leap year, unless:
        The year is also …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
1
解决办法
3万
查看次数

如何在涉及request.user.is_authenticated()和bool对象不可调用的Python Django中修复此错误?

我正在尝试为每个用户制作个人资料页面.我添加了一个代码,用于检查用户是否已登录并执行重定向(请参阅下面代码的第12行).

from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect, HttpResponse

from .models import Account, ForSale, WTB

from mysite.forms import MyRegistrationForm

def signup(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/user/')
    else:
        if request.method == 'POST':
            form = MyRegistrationForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/user/')

        context = {}
        context.update(csrf(request))
        context['form'] = MyRegistrationForm()

        return render(request, 'signup.html', context)

def index(request):
    return render(request, 'index.html')
Run Code Online (Sandbox Code Playgroud)

但是,在访问/注册/网站时,我收到以下调试消息:

TypeError at /signup/
'bool' object is not callable
Request Method: GET
Request URL:    http://url:8000/signup/
Django …
Run Code Online (Sandbox Code Playgroud)

django python-3.x

3
推荐指数
1
解决办法
1269
查看次数

Python 3:我的装饰函数运行两次

我有一些简单的斐波那契序列函数,我正在练习单元测试并使用Travis-CI/Docker构建:

fib_recursive.py:

from fib.fib import benchmark, fib_rec_memo

@benchmark
def print_fib(n):
    for x in range(0, n):
        print(fib_rec_memo(x))

print_fib(100)
Run Code Online (Sandbox Code Playgroud)

这是fib.fib导入源代码:

from time import time
from functools import wraps

def benchmark(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        t = time()
        func(*args, **kwargs)
        print(func.__name__, 'took:', time() - t)
        return func(*args, **kwargs)
    return wrapper


def fib_rec_memo(n, hash = {0:1, 1:1}):
    if n not in hash:
        hash[n] = fib_rec_memo(n-1) + fib_rec_memo(n-2)
    return hash[n]


@benchmark
def fib_standard(num):
    a, b = 0, 1
    c = []
    while a < num: …
Run Code Online (Sandbox Code Playgroud)

python function python-3.x

0
推荐指数
1
解决办法
39
查看次数

标签 统计

python ×2

python-3.x ×2

django ×1

function ×1