gpt4 book ai didi

Python If 语句在测试 1 到 10 之间的数字时不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:23 25 4
gpt4 key购买 nike

尝试让 if 语句发挥作用。

from __future__ import unicode_literals

from django.shortcuts import render

def index(request):
return render(request, 'landscapes/index.html')

def landscape(request, id):
print id
if id>=1 and id<=10:
print 'true'
landscape = 'https://wildwithgrace.files.wordpress.com/2010/12/snowy-landscape.jpg'
context = {
'landscape': landscape
}
return render(request, 'landscapes/landscape.html', context)

即使数字大于 1 且小于 10,也只会在/# 处返回 UnboundLocalError。

url.py:

from django.conf.urls import url, include
from . import views

urlpatterns = [
url(r'^$', views.index),
url(r'^(?P<id>\d+)$', views.landscape),
]

最佳答案

def landscape(request, id):
print id
if int(id) >= 1 and int(id) <= 10:
print 'true'
landscape = 'https://wildwithgrace.files.wordpress.com/2010/12/snowy-landscape.jpg'
context = {
'landscape': landscape
}
else:
context = {}
return render(request, 'landscapes/landscape.html', context)

试试这个

关于Python If 语句在测试 1 到 10 之间的数字时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45251478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com