Python 用户输出的anser将以元音打印的程序
我想打印一个用户输入,输出的是元音 结果将是: Enter a sentence: my name is Mike do you want to input more:yes Enter a sentence: i am from China Do you want to input more:no [a,e,i,i,e,i,a,o,i,a]Python 用户输出的anser将以元音打印的程序,python,python-3.x,Python,Python 3.x,我想打印一个用户输入,输出的是元音 结果将是: Enter a sentence: my name is Mike do you want to input more:yes Enter a sentence: i am from China Do you want to input more:no [a,e,i,i,e,i,a,o,i,a] 由于您提到用户输入应该仅为yes/no或yes/no,因此我还添加了一些使用输入和验证输入的方法 def validate_input(inp2):
由于您提到用户输入应该仅为yes/no或yes/no,因此我还添加了一些使用输入和验证输入的方法
def validate_input(inp2):
if inp2 == 'yes' or inp2 == 'YES' or inp2 == 'no' or inp2 == 'NO':
return True
else:
return False
def take_input(self):
print("Enter a sentence: ")
inp = input()
self.vowels.extend([i for i in inp.lower() if i in 'aeiou'])
class PrintVowels():
def __init__(self):
self.vowels = []
def get_vowels(self):
take_input(self)
while True:
print("Do you want to input more?")
inp2 = input()
valid_inp = validate_input(inp2)
if valid_inp:
if inp2.lower() == 'no':
break
else:
take_input(self)
else:
print("Error: Please try again with a valid input!")
continue
return self.vowels
ob1 = PrintVowels()
print (ob1.get_vowels())
输出:
Enter a sentence:
my name is Mike
Do you want to input more?
blahhhhWHAAT
Error: Please try again with a valid input!
Do you want to input more?
oops
Error: Please try again with a valid input!
Do you want to input more?
yes
Enter a sentence:
I am from China
Do you want to input more?
Huhhhh
Error: Please try again with a valid input!
Do you want to input more?
NO
['a', 'e', 'i', 'i', 'e', 'i', 'a', 'o', 'i', 'a']
到目前为止你有什么代码?您在任务的哪一部分遇到困难?问题不清楚,请明确指定您的输入内容以及您希望从该输入中得到什么。@vinodsesetti看起来输出只是按顺序列出组合输入中的所有元音。只需遍历sting并确定每个位置的字母表即可。如果遇到元音,请将其添加到列表中。最后只需打印列表。@CallMe programmer,下面的答案有助于解决您的问题吗?如果是这样,您可以标记您想要的,谢谢!
Enter a sentence:
my name is Mike
Do you want to input more?
blahhhhWHAAT
Error: Please try again with a valid input!
Do you want to input more?
oops
Error: Please try again with a valid input!
Do you want to input more?
yes
Enter a sentence:
I am from China
Do you want to input more?
Huhhhh
Error: Please try again with a valid input!
Do you want to input more?
NO
['a', 'e', 'i', 'i', 'e', 'i', 'a', 'o', 'i', 'a']