Python脚本,用于为科学展览会展位创建自定义打印材料

2024-09-29 21:20:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在准备在我组织即将举行的一个科学展览会上经营一个摊位。我正在帮助的booth围绕着使用Python实现手动过程和自动化。你知道吗

所有的展台都在采购自己的“好东西”,作为人们参观展台的奖励。你知道吗

我认为制作定制的Koozies是一个有趣的主意,它有一个Python脚本,可以创建一个函数来决定一个人是否应该使用Koozie。你知道吗

我基本上希望函数执行以下操作:

  1. 首先评估外界温度(temp)是否足够热,需要进行一次冷却
  2. 接下来评估是否是工作日,是否晚于下午5:00

以下是我目前掌握的情况:

import datetime
from time import gmtime, strftime

def koozie_decider(temp):

     if datetime.datetime.today().weekday() > 0 and datetime.datetime.today().weekday() < 5:
         if int(strftime("%H", gmtime())) >= 21:
             a = print("Outside of working hours - grab a koozie!")
         else:
             a = print("It's 5 o'clock somewhere - grab a koozie!")
     else:
         a = print("It's the weekend - grab a koozie!")
     if temp >= 72:
         b = print("And it's hot out - you're going to need one ASAP!")
     else:
         b = ""
     return a, b

理想情况下,我可以消除对最后一个“else”语句的需要(当temp低于72时仍然不返回字符串)。有人知道我怎么做吗?你知道吗

我也想让你们用语法来解释。展会上的人对Python的了解会比我多,所以我希望代码尽可能高效。你知道吗

有没有什么低挂的水果,让这个功能更“Python”?你知道吗


Tags: 函数importtodaydatetimeif情况ittemp
1条回答
网友
1楼 · 发布于 2024-09-29 21:20:34

你可以用很多不同的方法做很多事情。我很惊讶这个问题仍然悬而未决,不管怎么说,就像大家在评论中解释的那样,这样做毫无意义a = print('something...')

>>> t = print()

>>>
>>> a = print('10')
10
>>> a
>>> type(a)
<class 'NoneType'>
>>> def x():
...   a = print('10')
...   return a
...
>>> i = x()
10
>>> type(i)
<class 'NoneType'>
>>>

如果您仍然不完全理解这里发生了什么,您可以问不同的问题,因为这意味着您不知道print函数(python3.x)是如何工作的。

# pip install weather-api
import datetime
from time import localtime
from weather import Weather, Unit

def koozie_decider(day_idx = localtime().tm_wday, day_hr = localtime().tm_hour, temp = None):

     if temp == None:
         weather = Weather(unit=Unit.FAHRENHEIT)
         location = weather.lookup_by_location('dublin')
         temp = int(location.condition.temp)

     # day_idx = 0-4 mon-fri, | weekend day_idx = 5 and sat day_idx = 6 sun
     if day_idx > 4:
         a = "It's the weekend - grab your favorite Beverage! :D"
     else:
         # if working hrs is between 9( 9) - 5( 17) 24 hr clock
         if 8 < day_hr < 17 :
             a = "Dude it's past 5 somewhere - grab your favorite Beverage, but no Alcohol :( still at work!"
         else:
             a = "Outside of working hours - grab your favorite drink! :D"

     if temp >= 72:
         b = "And it's hot out - grab a koozie, you're going to need one ASAP!"
     else:
         b = "And grab a koozie anyway - coz koozie is lub ;)" 

     return a, b


a,b = koozie_decider(0,10,78) # monday, 10 something, 78 F
print(a,b,sep = '\n')


Dude it's past 5 somewhere - grab your favorite Beverage, but no Alcohol :( still at work!
And it's hot out - grab a koozie, you're going to need one ASAP!


这将看起来有点酷,因为反对给自己的温度。尽管位置仍然需要硬编码。你知道吗

a,b = koozie_decider()
print(a,b,sep = '\n')

Outside of working hours - grab your favorite drink! :D
And grab a koozie anyway - coz koozie is lub ;)

修改代码koozie_decider。你知道吗

相关问题 更多 >

    热门问题