打印,循环,缩进,Python(Print, loop, Indentation, Python)

编程入门 行业动态 更新时间:2024-10-10 17:25:41
打印,循环,缩进,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.

更多推荐

本文发布于:2023-07-29 03:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1311953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Print   Python   Indentation   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!