Logic 1 < squirrel_play
|
def squirrel_play(temp, is_summer):
if not is_summer and (temp<=90 and temp>=60):
return True
elif is_summer and (temp<=100 and temp>=60):
return True
else:
return False
写完后感觉很啰嗦,搜索到了一个if...else的简写版本
def squirrel_play(temp, is_summer):
return 60 <= temp <= (100 if is_summer else 90)
实例:Logic 1 < sorta_sum
|
def sorta_sum(a, b):
if 10<=a+b<=20:
return 20
else:
return a+b
#以下是简写后
def sorta_sum(a, b):
return 20 if 10<=a+b<=20 else a+b
Logic 1 < alarm_clock
|
def alarm_clock(day, vacation):
while not vacation:
if 0
这道题很简单,但是github上咋还有人这么写,思路清晰,值得学习
def alarm_clock(day, vacation):
weekdays = "10:00" if vacation else "7:00"
weekends = "off" if vacation else "10:00"
if 1 <= day and day <= 5:
return weekdays
return weekends
#https://github.com/mirandaio/codingbat/blob/master/python/logic-1/alarm_clock.py#L5
in1to10
def in1to10(n, outside_mode):
if outside_mode:
return n<=1 or n>=10
else:
return 1<=n<=10
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
in1to10(5, False) → True
in1to10(11, False) → False
in1to10(11, True) → True


![[Python学习日记]-coding bat难题记录- Logic 1 [Python学习日记]-coding bat难题记录- Logic 1](http://www.mshxw.com/aiimages/31/916137.png)
