【问题标题】:How to solve this using while loop如何使用while循环解决这个问题
【发布时间】:2019-12-04 07:21:23
【问题描述】:

家庭作业:

考虑包含值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 循环时犯了一些错误。

【问题讨论】:

  • 请把你的代码和回答内容用for循环函数,更容易理解你想要什么或需要什么。
  • 它将帮助您处理您的问题URL

标签: python python-3.x


【解决方案1】:
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 循环将终止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2021-07-28
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多