我正在尝试使用以下代码进行计算:
def traffic_intensity(count):
"""Number of cars passing by"""
int(count)
if count < 5000:
level = "Very Low"
elif 5000 <= count < 10000:
level = "Low"
elif 10000 <= count < 18000:
level = "Moderate"
elif count >= 18000:
level = "High"
return level
def number_of_busy_days(counts):
"""Busy days based on traffic flow"""
daily_counts = 0
for count in counts:
if traffic_intensity(level) == "Moderate" or "High":
daily_counts = daily_counts + 1
return daily_counts
counts = [18000, 10000, 500, 9999, 12000]
print(number_of_busy_days(counts))
我想要实现的是使用traffic_intensity函数来计算忙碌天数 - 忙碌的一天被定义为具有超过traffic_intensity函数中给出的“中等”流量的量。我尝试了很多不同的方法,但此时我的想法已经不多了。
我遇到的问题是它没有从第一个函数中找到级别变量。我收到以下错误:
if traffic_intensity(level) == "Moderate" or "High":
NameError: name 'level' is not defined
有人能帮助我吗?谢谢! ^ _ ^
答案 0 :(得分:0)
您传入的函数traffic_intensity中不存在变量level
。我认为你打算传入变量count
。
同样,将or "High"
作为条件将始终为true,因为非空字符串在python中可转换为true
def traffic_intensity(count):
"""Number of cars passing by"""
int(count)
if count < 5000:
level = "Very Low"
elif 5000 <= count < 10000:
level = "Low"
elif 10000 <= count < 18000:
level = "Moderate"
elif count >= 18000:
level = "High"
return level
def number_of_busy_days(counts):
"""Busy days based on traffic flow"""
daily_counts = 0
for count in counts:
state = traffic_intensity(count)
if state == "Moderate" or state == "High":
daily_counts = daily_counts + 1
return daily_counts
counts = [18000, 10000, 500, 9999, 12000]
print(number_of_busy_days(counts))