作业:
考虑包含值
Great things never come from comfort zones
的字符串 's' 。使用
while
循环确定其中存在的元音编号。将数字存储在变量count
中,然后打印出来。
法典:
s="Great things never come from comfort zones"
vowels=["a","e","i","o","u"]
count=0
while chars in s:
if chars in vowels:
count=count+1
我知道如何使用for
循环来解决这个问题,但试图在循环中解决这个问题while
但出现错误。
我想我在增加while
循环时犯了一些错误。
s = "Great things never come from comfort zones"
vowels = ["a","e","i","o","u"]
count = 0
while len(s) > 0:
char = s[-1] # assign last character of 's' to 'char' important to do this first
s = s[:-1] # now remove the last character from 's'
if char in vowels:
count += 1
print(count)
您需要定义一个条件来终止 while 循环。在这里,我们从"s"中剪切字符,直到它为空(s = '' 或 len(s( = 0(,然后 while 循环将终止。