Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环和while循环问题。结果不是';这不是我所期望的_Python_Python 3.x_For Loop_While Loop - Fatal编程技术网

Python for循环和while循环问题。结果不是';这不是我所期望的

Python for循环和while循环问题。结果不是';这不是我所期望的,python,python-3.x,for-loop,while-loop,Python,Python 3.x,For Loop,While Loop,一般来说,我对Python和Stackoverflow都是新手,所以如果我的格式很糟糕,而且我的英语也不好,那么很抱歉。但是我对这个代码有一个问题 w = input('Please enter a word: ') total = 0 for i in w: if i in 'AEIOUaeiou': print(i,end='') 这是代码的结果 Please enter a word: Elephant Eea 这是工作,但我不知道如何让结果像这样进行 Ple

一般来说,我对Python和Stackoverflow都是新手,所以如果我的格式很糟糕,而且我的英语也不好,那么很抱歉。但是我对这个代码有一个问题

w = input('Please enter a word: ')
total = 0
for i in w:
     if i in 'AEIOUaeiou':
        print(i,end='')
这是代码的结果

Please enter a word: Elephant
Eea
这是工作,但我不知道如何让结果像这样进行

Please enter a word: Elephant
Total vowel found = 3
Eea
Total consonant found = 5
lphnt
输入:大象

输出:

Please enter a word: Elephant
Total vowel found =  3
Eea 
Total consonant found =  5
lphnt 
输入:大象

输出:

Please enter a word: Elephant
Total vowel found =  3
Eea 
Total consonant found =  5
lphnt 

以下是代码的解决方案:) 你只需要一些额外的格式就够了

word_format = input("Please enter a word: ")
total_vowels = 0
total_constants = 0
vowel_list = []
constant_list = []
for check in word_format:
    if check in "AEIOUaeiou":
        total_vowels += 1
        vowel_list.append(check)
    else:
        total_constants += 1
        constant_list.append(check)


# Here is our capture:
print(f"Total vowels found: {total_vowels}")
print(''.join(vowel_list))
print(f"Total constants found: {total_constants}")
print(''.join(constant_list))
这是结果

Please enter a word: Elephant
Total vowels found: 3
Eea
Total constants found: 5
lphnt

以下是代码的解决方案:) 你只需要一些额外的格式就够了

word_format = input("Please enter a word: ")
total_vowels = 0
total_constants = 0
vowel_list = []
constant_list = []
for check in word_format:
    if check in "AEIOUaeiou":
        total_vowels += 1
        vowel_list.append(check)
    else:
        total_constants += 1
        constant_list.append(check)


# Here is our capture:
print(f"Total vowels found: {total_vowels}")
print(''.join(vowel_list))
print(f"Total constants found: {total_constants}")
print(''.join(constant_list))
这是结果

Please enter a word: Elephant
Total vowels found: 3
Eea
Total constants found: 5
lphnt

事实上,问题出在if语句中,在if语句之后,您正在打印“i”,即“AEIOUaeiou”中出现的输入单词的字母,这就是为什么它只打印元音。使用此代码:-

w = input('Please enter a word: ')
vowels=""
consonent=""
for i in w:
      if i in 'AEIOUaeiou':
         vowels+=i
      else:
         consonent+=i
print (f"total vowels found={len(vowels)}")
print(vowels)
print (f"total consonents found ={len(consonent)}")
print(consonent)
现在,输出将如下所示:

Please enter a word: elephant
total vowels found=3
eea
total consonents found=5
lphnt

我希望这将对u有帮助

事实上,问题在于if语句中,在if语句之后,您正在打印“i”,即输入单词的字母,它们出现在“AEIOUaeiou”中,这就是为什么它只打印元音。使用此代码:-

w = input('Please enter a word: ')
vowels=""
consonent=""
for i in w:
      if i in 'AEIOUaeiou':
         vowels+=i
      else:
         consonent+=i
print (f"total vowels found={len(vowels)}")
print(vowels)
print (f"total consonents found ={len(consonent)}")
print(consonent)
现在,输出将如下所示:

Please enter a word: elephant
total vowels found=3
eea
total consonents found=5
lphnt

我希望这会对你有帮助,你不需要任何循环。只需使用过滤器和连接。它会加速你的代码

w = "Elephant"

v="".join(filter(lambda x: x in "AEIOUaeiou", w))
c="".join(filter(lambda x: x not in "AEIOUaeiou", w))

print(v, len(v))
print(c, len(c))

这不需要任何循环。只需使用过滤器和连接。它会加速你的代码

w = "Elephant"

v="".join(filter(lambda x: x in "AEIOUaeiou", w))
c="".join(filter(lambda x: x not in "AEIOUaeiou", w))

print(v, len(v))
print(c, len(c))

如果你想计算一些东西,你需要增加
total
。并打印结果。另外,也不确定while循环从何而来。如果要计数,则需要增加
total
。并打印结果。也不确定while循环在哪里