python - 在 Python 3 中定义函数:返回 None 值
问题描述
我编写了一个函数,旨在根据用户自己输入的日期和时间告诉您用户依赖于哪个成本段。
一切似乎都很好,但我不太明白为什么函数在正确和预期的输出之后返回 None 输出。
请在下面查看我的代码:
day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
hour = list(range(0, 24))
segments = {'Cheap': hour[:8], 'Intermediate': hour[8:10] + hour[14:18] + hour[22:], 'Costly': hour[10:14] + hour[18:22]}
dayEvaluate = "z"
hourEvaluate = 25
#Let's define the function:
def power_price_calculator(dayEvaluate, hourEvaluate):
while (dayEvaluate not in day):
dayEvaluate = input('Enter the day you want to check: ')
if dayEvaluate not in day:
print('Please, enter a valid day name (Monday-Sunday)')
if dayEvaluate in day[:4]:
while (hourEvaluate not in hour):
hourEvaluate = int(input('Enter the hour you want to check: '))
if((hourEvaluate > 24) or (hourEvaluate < 0)):
print('Please, enter a valid hour (0-24h)')
if hourEvaluate in segments['Cheap']:
print('Congratulations! You are in the cheap segment')
elif hourEvaluate in segments['Intermediate']:
print('It is OK, you are in the intermediate segment')
elif hourEvaluate in segments ['Costly']:
print('Beware of your consumption, you are in the costly segment')
else:
print('Congratulations! You are in the cheap segment')
print(power_price_calculator(dayEvaluate, hourEvaluate))
任何人都可以对此有所了解吗?
非常感谢!
解决方案
像这样拆分您的问题:
result = power_price_calculator(dayEvaluate, hourEvaluate) # result is None
print(result) # Does not print anything
这是因为你的函数只打印数据,它不返回任何东西
推荐阅读
- go - 查找整数位数的最快方法?
- autodesk-forge - 映射到 Forge Viewer
- flutter - 如何让 Flutter Widget 在特定时间(以天为单位)后消失?
- android - 如何以编程方式更改系统导航?
- python - 如何在 2 个模型(表)中创建具有基于一种形式的可重用函数的行
- c++ - C++ TCP发送错误:无法解析不存在的文件'/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/send.c'
- python - pandas-内核重新启动:.ipynb 的内核似乎已经死了。它会自动重启
- java - Akka Streams 异步运行流
- sql - 在多个匹配的情况下选择具有 WHERE 条件的随机记录
- python - ValueError:形状(6497,12)和(6497,12)未对齐:12(dim 1)!= 6497(dim 0)