python - How do i return a string if a list is empty in python
问题描述
I'm fairly new to coding, but what this project is asking, is to complete the function so that if a list is empty, it returns the string "does not exist" but currently, it comes up with a bunch of errors. How do I go about adding a function within my lowest_number definition that returns a string if a list is empty (for example, list6)
def lowest_number(num_list):
lowest = num_list[0]
for x in num_list:
if x < lowest:
lowest = x
return lowest
解决方案
change the lowest_number
to look like:
Adding if conditon to check the length of the list
def lowest_number(num_list):
if (len(num_list) < 1): # change over here
return "does not exist" # change over here
lowest = num_list[0]
for x in num_list:
if x < lowest:
lowest = x
return lowest
推荐阅读
- javascript - Kintone日历事件范围显示颜色
- c++11 - 如何一起编译不同的依赖proto
- python - 动态创建列表(即时)
- spring-cloud - 尽管启用了机制,Zuul 不会重试失败的请求
- python - 如何按名称编辑列
- python - 如何使用请求在 Python 中发布多部分/表单数据
- altium-designer - 如何在 Altium 中移动工作表边框并与网格对齐
- r - 用于在 R 中的数据框中计算和添加新列的函数
- micrometer - MicroMeter LongTaskTimer如何在方法失败时清除样本
- java - Spring Boot 如何找到控制器包?