如果prod_Available < 0
(负值)并且返回status = 0
,我怎么能停止循环呢?否则它会返回status = 1
?是否有可能当我的循环遇到prod_Available < 0
时,它将停止循环并返回status = 0
。?
这是我的代码:
def get_hotel_sum_quantity(self, product_id, checkin_date, checkout_date, quantity):
check_in = datetime.datetime.strptime(checkin_date, '%Y-%m-%d')
check_in = check_in.date()
start_date = check_in.day
check_out = datetime.datetime.strptime(checkout_date, '%Y-%m-%d')
check_out = check_out.date()
end_date = check_out.day
prod = Product.objects.get(id=product_id)
prod_Available = 0
array = []
for x in range(start_date,end_date + 1):
x = x - start_date
delta = datetime.timedelta(days=x)
all_date = check_in + delta
sumOfQuantity = HotelCheck.objects.filter(date_booked=all_date, product=prod).aggregate(Sum('quantity'))['quantity__sum']
if sumOfQuantity == None:
sumOfQuantity = 0
prod_Available = prod.quantity - sumOfQuantity
prod_Available = float(prod_Available) - float(quantity)
if prod_Available < 0:
status = 0
else:
status = 1
data = {'available': prod_Available, 'status': status, 'sumquantity': sumOfQuantity, 'quanity': quantity}
array.append(data)
return array
我已经返回了所有值,但在prod_Available < 0
时它不会停止循环
这就是我所做的:
>>> from booking.models import *
>>> Hotel.objects.get_hotel_sum_quantity(3, '2011-12-21', '2011-12-24', 1)
[{'available': 1.0, 'status': 1, 'sumquantity': 8, 'quanity': 1}, {'available': 1.0, 'status': 1, 'sumquantity': 8, 'quanity': 1}, {'available': 1.0, 'status': 1, 'sumquantity': 8, 'quanity': 1}, {'available': 9.0, 'status': 1, 'sumquantity': 0, 'quanity': 1}]
>>> Hotel.objects.get_hotel_sum_quantity(3, '2011-12-21', '2011-12-24', 2)
[{'available': 0.0, 'status': 1, 'sumquantity': 8, 'quanity': 2}, {'available': 0.0, 'status': 1, 'sumquantity': 8, 'quanity': 2}, {'available': 0.0, 'status': 1, 'sumquantity': 8, 'quanity': 2}, {'available': 8.0, 'status': 1, 'sumquantity': 0, 'quanity': 2}]
>>> Hotel.objects.get_hotel_sum_quantity(3, '2011-12-21', '2011-12-24', 3)
[{'available': -1.0, 'status': 0, 'sumquantity': 8, 'quanity': 3}, {'available': -1.0, 'status': 0, 'sumquantity': 8, 'quanity': 3}, {'available': -1.0, 'status': 0, 'sumquantity': 8, 'quanity': 3}, {'available': 7.0, 'status': 1, 'sumquantity': 0, 'quanity': 3}]
>>>
任何人都可以给我一个关于我情况的想法吗?
提前致谢
答案 0 :(得分:3)
Mayb你可以使用python的break语句来阻止循环:
if prod_Available < 0:
status = 0
else:
status=1
data = {'available': prod_Available, 'status': status, 'sumquantity': sumOfQuantity, 'quanity': quantity}
array.append(data)
if status == 0:
break
然后,当prod_available低于0时,它将停止循环并且您的方法将继续执行(并在您的情况下返回数组)
答案 1 :(得分:0)
而不是
for x in range(start_date,end_date + 1):
x = x - start_date
............
你应该写:
for x in range(end_date -start_date + 1):
............
但还有其他一些方面需要改进 这是我的主张:
import datetime
from time import strptime
def get_hotel_sum_quantity(product_id, checkin_date, checkout_date, quantity):
check_in = datetime.date(*strptime(checkin_date, '%Y-%m-%d')[0:3])
check_out = datetime.date(*strptime(checkout_date, '%Y-%m-%d')[0:3])
prod = Product.objects.get(id=product_id)
prod_Available = 0
array = []
all_date = check_in
one_day = datetime.timedelta(days=1)
for i in xrange(check_in.day,check_out.day + 1):
sumOfQuantity = HotelCheck.objects.filter(date_booked=all_date, product=prod).aggregate(Sum('quantity'))['quantity__sum']
if sumOfQuantity == None:
sumOfQuantity = 0
prod_Available = prod.quantity - sumOfQuantity
prod_Available = float(prod_Available) - float(quantity)
if prod_Available > 0:
array.append({'available': prod_Available, 'status': 1, 'sumquantity': sumOfQuantity, 'quanity': quantity})
all_date += one_day
else:
return array
如果你想要一个元素{'available':-1.0,'status':0,'sumquantity':8,'quanity':3}要包含并显示在回复中,你可以:
if prod_Available > 0:
array.append({'available': prod_Available, 'status': 1, 'sumquantity': sumOfQuantity, 'quanity': quantity})
all_date += one_day
else:
array.append({'available': prod_Available, 'status': 0, 'sumquantity': sumOfQuantity, 'quanity': quantity})
return array
我认为如果我们知道HotelCheck.objects和Sum('quantity')的代码,我们也可以改进使用它们的部分 (请,任何人说正确的英语:纠正这句话,动词有问题,但我找不到正确的方法)
我的代码有一些缺点,我改进了它:
def get_hotel_sum_quantity(self,product_id, checkin_date, checkout_date, quantity,
daylta = datetime.timedelta(days=1)):
all_date = datetime.date(*strptime(checkin_date, '%Y-%m-%d')[0:3])
check_out_day = strptime(checkout_date, '%Y-%m-%d')[2]
prod = Product.objects.get(id=product_id)
prod_quant = prod.quantity
array = []
for i in xrange(all_date.day,check_out_day + 1):
sumOfQuantity = HotelCheck.objects\
.filter(date_booked=all_date,product=prod)\
.aggregate(Sum('quantity'))['quantity__sum']
if sumOfQuantity == None: sumOfQuantity = 0
prod_Available = float(prod_quant - sumOfQuantity) - float(quantity)
if prod_Available > 0:
array.append({'available': prod_Available, 'status': 1,
'sumquantity': sumOfQuantity, 'quantity': quantity})
all_date += daylta
else:
break
return array
答案 2 :(得分:0)
你应该使用break语句来停止循环!注意break语句会中断你调用它的循环的执行,
否则,如果你不喜欢break,你可以手动设置x超出范围,但是代码将更难被阅读。
您可能希望在最后打破循环(即使状态为0,您是否要将数据附加到数组?)
array.append(data)
if prod_Available < 0:
break