打印,循环,缩进,Python(Print, loop, Indentation, Python)
我需要一点帮助。 我目前正在学习python,我有python 2.7.8我正在寻找一个简单的程序来帮助计算我的单词中的元音。 这是代码:
count = 0 total = 0 for v in "bonbon": count += 1 if v == 'e' or v == 'o' or v == 'u' or v == 'a': print('the number of vowel in your word is ' +str(total))为什么要打印两次? 1-我的元音数是0,然后我的数是2
有人可以帮帮我吗? 多谢你们
I need a pit of help. I am currently learning python, and I have python 2.7.8 I am looking to build a simple program that will help to count the vowels in my word. Here is the code:
count = 0 total = 0 for v in "bonbon": count += 1 if v == 'e' or v == 'o' or v == 'u' or v == 'a': print('the number of vowel in your word is ' +str(total))Why does it print twice? 1- the number of my vowel is 0 and then the number of my is 2
Could someone help me please? Thanks guys
最满意答案
它打印两次,因为你在for循环中有打印。 您应该在for循环内增加total,然后再打印它。 如果你这样做:
count = 0 total = 0 for v in "bonbon": count += 1 if v == 'e' or v == 'o' or v == 'u' or v == 'a': total += 1 print('the number of vowel in your word is ' + str(total))它应该工作。
It's printing twice because you have the print inside of the for loop. You should instead increment total inside the for loop and then print it afterwards. If you do:
count = 0 total = 0 for v in "bonbon": count += 1 if v == 'e' or v == 'o' or v == 'u' or v == 'a': total += 1 print('the number of vowel in your word is ' + str(total))It should work.
更多推荐
发布评论