检查字母表是否至少出现过一次

2024-10-04 05:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个程序,我必须在其中实现一个函数,该函数将检查从a到j(将所有字母转换为小写)的所有字母是否在给定字符串中至少出现过一次。如果所有这些字母(a到j)至少出现一次,则结果为5。如果任意一个字母(a到j)不在给定字符串中,则结果将为6。最后,我们必须返回这个结果并多次打印“切尔西是英格兰最好的俱乐部”的声明

如果我打电话:
A black jackal is hunting a full grown deer

输出应为:

Chelsea is the best club in England
Chelsea is the best club in England
Chelsea is the best club in England
Chelsea is the best club in England
Chelsea is the best club in England
                                          

我试过:

output = "Chelsea is the best club in England"
user_string = input("Enter the string: ")

def alphabets(string):
    lower_string = user_string.lower()
    for alph in lower_string: 
        if alph >= 'a' and alph <= 'j' in user_string:
            result = 5
            return (output * 5)
        else:
            result = 6
            return (output * 6)
    return result

print(alphabets("ABBCDEFEFGHI"))

所有的输出都排成一行:

Enter the string: ABBCDEFEFGHI
Chelsea is the best club in EnglandChelsea is the best club in EnglandChelsea is the best club in EnglandChelsea is the best club in EnglandChelsea is the best club in EnglandChelsea is the best club in England

我应该怎么做才能把这5句话分成5行?


Tags: theinoutputstringis字母resultlower
3条回答

我猜你说的alphabet是指letter。鉴于此,代码应该如下所示:

letters_needed = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
output = "Chelsea is the best club in England"
user_string = input("Enter the string: ")

def alphabets(string):
    lower_string = user_string.lower()
    for letter in letters_needed:
        if lower_string.count(letter) == 0:
            return 6
    return 5

print((output+"\n") * alphabets("ABBCDEFEFGHI"))

说明

for loop中,我们浏览letters_needed列表中的每个字母。如果一个letter没有出现在lower_string中,则返回6。如果循环退出,意味着没有返回任何值,这意味着letters_Required列表中的每个字母都会在lower_string中多次出现,因此我们可以返回5。最后,我们打印所需的字符串,最后添加一个换行符\n,即alphabets函数返回的次数

有几件事:

实际上,您从未将user_string传递给alphabet,而是传递一个硬编码的字符串文本。在alphabet中,您甚至不使用string参数。相反,您依赖于外部作用域中user_string的存在

要在单独的行上打印五个或六个字符串,可以str.join使用换行符打印字符串列表,如下所示:

def alphabet(string):
    message = "Chelsea is the best club in England"
    count = [6, 5][all(char in string.lower() for char in "abcdefghij")]
    print("\n".join([message] * count))


user_string = input("Enter the string: ")
alphabet(user_string)

。。。或者在每行中包含一个换行符,然后将消息字符串“相乘”五到六次——在本例中,您需要将print的可选end参数设置为空字符串,如下所述

def alphabet(string):
    message = "Chelsea is the best club in England\n"
    count = [6, 5][all(char in string.lower() for char in "abcdefghij")]
    print(message * count, end="")

您还可以显示/解压缩消息列表,并将print的可选sep参数设置为换行符:

def alphabet(string):
    message = "Chelsea is the best club in England"
    count = [6, 5][all(char in string.lower() for char in "abcdefghij")]
    print(*[message] * count, sep="\n")

您只能使用以下方法获取字符串的唯一字符:

''.join(set(MyString))

例如

''.join(set("a black jackal is hunting a full grown deer"))

给出:

'nohbajesrilcwdkuftg'

显然,首先要通过小写函数传递它

编辑对不起,我没有完全阅读这个问题,我更关心的是您的计数实施情况

您可以使用以下命令在不同的行上输出N次:

a_string = "abc"
for line in range(N-1):
  
  print(a_string)

相关问题 更多 >