简体   繁体   English

如何在 Python 中选择字符串中的某些字符?

[英]How to select certain characters in a string in Python?

My name is Shaun.我叫肖恩。 I am 13 years old and trying to learn python.我今年 13 岁,正在努力学习 python。

I am trying to make a program that finds vowels in an input and then prints how many vowels there are in the input the user gives.我正在尝试制作一个程序,该程序在输入中查找元音,然后打印用户提供的输入中有多少元音。

Here is the code:这是代码:

s = (input('Enter a string: ')) # Let the user give an input (has to be a string)

Vwl = [] # Create an array where we will append the values when the program finds a vowel or several vowels

for i in s: # Create a loop to check for each letter in s 
   count_a = 0 # Create a variable to count how many vowels in a

   count_e = 0 # Create a variable to count how many vowels in e

   count_i = 0 # Create a variable to count how many vowels in i

   count_o = 0 # Create a variable to count how many vowels in o

   count_u = 0 # Create a variable to count how many vowels in u

The function below is pretty long to explain, so summary of the function below is to find a vowel in s (the input) and make one of the counters, if not some or all, increase by 1. For the sake of learning, we append the vowels in the array Vwl.下面的函数解释起来很长,所以下面的函数总结是在 s(输入)中找到一个元音,并使计数器之一,如果不是部分或全部,则增加 1。为了学习,我们将元音附加到数组 Vwl 中。 Then, it prints out Vwl and how many letters there are in the list by using len.然后,它使用 len 打印出 Vwl 以及列表中有多少个字母。

   if s.find("a" or "A") != -1: 
       count_a = count_a + 1    
       Vwl.append('a')          

   elif s.find("e" or "E") != -1:
       count_e = count_e + 1
       Vwl.append("e")
   elif s.find("i" or "I") != -1:
       count_i = count_i + 1
       Vwl.append("i")
   elif s.find("o" or "O") != -1:
       count_o = count_o + 1
       Vwl.append("o")
   elif s.find("u" or "U") != -1:
       count_u = count_u + 1
       Vwl.append("u")

  print(Vwl)
  print(f"How many vowels in the sentence: {len(Vwl)}")

For some odd reason however, my program first finds the first vowel it sees, and converts the whole string into the first vowel it finds.然而,出于某种奇怪的原因,我的程序首先找到它看到的第一个元音,并将整个字符串转换为它找到的第一个元音。 Then it prints down the wrong amount of vowels based on the array of vowels in the array Vwls然后它根据数组 Vwls 中的元音数组打印出错误的元音数量

Could someone please help me?有人可以帮我吗?

The reason your code only prints out the first vowel is because the if statements you coded are not inside a loop, that part of the code runs once and then it finishes, so it only manages to find one vowel.您的代码仅打印出第一个元音的原因是因为您编写的 if 语句不在循环内,该部分代码运行一次然后完成,因此它只能找到一个元音。 Here are couple ways you can do what you are trying to do:您可以通过以下几种方法来做您想做的事情:

Way 1: Here is if you just want to count the vowels:方式1:如果你只想计算元音:

s = input()
vowel_counter = 0
for letter in s:
    if letter in "aeiou":
        vowel_counter+=1
print(f"How many vowels in the sentence: {vowel_counter}")

Way 2: Use a python dictionary to keep track of how many of each vowel you have方式 2:使用 python 字典来跟踪每个元音有多少个

s = input()
vowel_dict = {}
for letter in s:
    if letter in "aeiou":
        if letter not in vowel_dict:
            vowel_dict[letter]=0
        vowel_dict[letter]+=1
print(f"How many vowels in the sentence: {sum(vowel_dict.values())}")
print(vowel_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM