我想写一个代码,让人输入一个句子,然后再检查元音和检查,还应该返回句子中的元音总数。到目前为止,我的情况是这样的,但是,我没有运行一次输入,而是多次获得输入,然后代码产生了以下错误:TypeError: 'NoneType' object is not iterable。
def vowel(func):
def wrapper():
vowels = "aeiouAEIOU"
vowel_count = 0
for items in func():
if items in vowels:
vowel_count += 1
print("Vowel found: " + items)
print("Total amount of vowels", vowel_count)
return wrapper
def censorship(func):
def wrapper():
censorship_list = ["Word", "Word1", "Word2", "Word3"]
for words in censorship_list:
if words in func():
print("You are not allowed to use those word(s) in set order: ", words)
return wrapper
@vowel
@censorship
def sentence():
sent = input("Input your sentence: ")
return sent
sentence()发布于 2018-04-14 18:20:00
有两个问题。首先,没有一个wrapper函数返回值。因此,当调用sentence时,censorship中的第一个wrapper将不会返回在vowel中使用的必要值。其次,func调用在vowel中将返回前一个装饰器返回的所有值(在censorship中);但是,即使censorship中的wrapper返回一个值,它也必须包含原始字符串输入的副本:
def vowel(func):
def wrapper():
vowels = "aeiouAEIOU"
vowel_count = 0
start_val = func()
for items in start_val:
if items in vowels:
vowel_count += 1
return start_val, vowel_count
return wrapper
def censorship(func):
def wrapper():
censorship_list = ["Word", "Word1", "Word2", "Word3"]
string = func()
for words in censorship_list:
if words in string:
raise ValueError("You are not allowed to use those word(s) in set order: ")
return string
return wrapper
@vowel
@censorship
def sentence():
sent = input("Input your sentence: ")
return senthttps://stackoverflow.com/questions/49834617
复制相似问题