Python编写元音检测函数return返回布尔值调用后无输出怎么办?
你的代码本身逻辑没有问题,没有输出是因为Python中函数的return语句仅会将运算结果返回给调用位置,不会自动把结果打印到控制台,你写的两种return写法都是符合语法规范的,不会影响执行结果。
你可以选择以下任意一种方式输出结果:
- 直接打印函数调用结果
def search4vowels(word): vowels = set('aeiou') found = vowels.intersection(set(word)) return bool(found) print(search4vowels('test'))
运行上述代码后控制台会输出True。
- 先接收返回值再打印
如果你后续还需要用到这个判断结果,可以先用变量存储返回值,需要打印时再输出:
def search4vowels(word): vowels = set('aeiou') found = vowels.intersection(set(word)) return bool(found) # 接收返回值 has_vowel = search4vowels('test') # 打印结果 print(has_vowel)
内容的提问来源于stack exchange,提问作者Kingster3742
相关产品推荐
相关产品推荐

