简体   繁体   English

Python 3 // 如果字符串不包含元音则打印错误

[英]Python 3 // Printing an error if a string contains no vowels

Write a function which takes a string argument with no spaces in it, searches for vowels (the letters "a", "e", "i", "o", "u") in the string, replaces them by upper case characters, and prints out the new string with the upper cases as well as returns the new string from the function.编写一个函数,它接受一个没有空格的字符串参数,在字符串中搜索元音(字母“a”、“e”、“i”、“o”、“u”),用大写字符替换它们, 并打印出大写的新字符串,并从函数中返回新字符串。 You should verify it is a string argument using isalpha (so no spaces are allowed!) and return with an error if not (the error message should being with "Error:").您应该使用isalpha验证它是一个字符串参数(因此不允许使用空格!),如果不是,则返回错误(错误消息应该带有“错误:”)。

For instance, if the string input is "miscellaneous", then your program will print out and return "mIscEllAnEOUs".例如,如果字符串输入是“miscellaneous”,那么您的程序将打印出来并返回“mIscEllAnEOUs”。 If nothing in the string is a vowel, print "Nothing to convert!"如果字符串中没有元音,则打印“Nothing to convert!” and return None .并返回None

This is what I have so far that is working, but I'm having trouble with the part in bold in the assignment.到目前为止,这就是我所拥有的,但我在作业中以粗体显示的部分遇到了麻烦。

def uppercase(word):
    vowels = "aeiou"
    error_msg = "Error: not a string."
    nothing_msg = "Nothing to convert!"  
   

    new_word = []
    
    for letter in word:
        
        if word.isalpha():
            if letter in vowels:
                new_word.append(letter.upper())
            
            else:
                new_word.append(letter.lower())
        
        else:
            print(error_msg)
            return None

    new_word = ''.join(new_word)
    return new_word

To check that a string is all letters you can use str.isalpha .要检查字符串是否全部为字母,您可以使用str.isalpha To check that there are vowels contained, you can use a generator expression within any to confirm that at least one of the letters is a vowel.要检查是否包含元音,您可以在any使用生成器表达式来确认至少一个字母是元音。 Then lastly you can do the conversion with another generator expression within join to uppercase only the vowels, then return a new string.最后,您可以使用join另一个生成器表达式进行转换,只将元音大写,然后返回一个新字符串。

def uppercase(word):
    if not word.isalpha():
        return 'Error'
    if not any(letter in 'aeiou' for letter in word):
        return 'Nothing to convert!'
    return ''.join(letter.upper() if letter in 'aeiou' else letter for letter in word)

Examples例子

>>> uppercase('miscellaneous')
'mIscEllAnEOUs'
>>> uppercase('abc123')
'Error'
>>> uppercase('xyz')
'Nothing to convert!'

Just to give a different approach, in addition to what CoryKramer answered, you could use a python re module:只是为了提供一种不同的方法,除了 CoryKramer 回答的内容之外,您还可以使用 python re模块:

import re

def uppercase(word):
    if not word.isalpha():
        return 'Error'
    if not any(letter in 'aeiou' for letter in word.lower()):
        return 'Nothing to convert!'
    return re.sub(r'[aeiou]', lambda m: m.group().upper(), word)

I think this is more concise.我觉得这样更简洁。

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

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