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

Python 检查字母表是否至少出现过一次,python,Python,这是一个程序,我必须在其中实现一个函数,该函数将检查从a到j(将所有字母转换为小写)的所有字母是否在给定字符串中至少出现过一次。如果所有这些字母(a到j)至少出现一次,则结果为5。如果任意一个字母(a到j)不在给定字符串中,则结果将为6。最后,我们必须返回这个结果并多次打印“切尔西是英格兰最好的俱乐部”的声明 如果我打电话: 一只黑豺狼正在猎杀一只成年鹿 输出应为: Chelsea is the best club in England Chelsea is the best club in E

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

如果我打电话:
一只黑豺狼正在猎杀一只成年鹿

输出应为:

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
我试过:

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行?

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

''.join(set(MyString))
e、 g

给出:

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

很抱歉,我没有完全阅读这个问题,我更关心的是你实施的计数

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

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

您可以在变量输出中添加一个换行符

 output = "Chelsea is the best club in England \n "
或者,您的返回语句也可以有一个换行符

if alph >= 'a' and alph <= 'j' in user_string:
            result = 5
            return ((output + "\n") * 5)

        else:
            result = 6
            return ((output + "\n") * 6)

如果alph>='a'和alph需要验证从
a
j
的所有内容,则不能直接返回内容,需要迭代所有字符串。如果单词case
5
中缺少一个字符,您只需要在case
6
末尾迭代从
a
j
的所有字符,因为您已经找到了所有字符

def alphabets(string):
    lower_string = string.lower()
    for c in 'abcdefghij':  
        if c not in lower_string:
            return "\n".join(5 * [output])
    return "\n".join(6 * [output])    
但是到目前为止,你能做的最简单的事情是

def alphabet(string):
    count = 6 if set("abcdefghij").issubset(string.lower()) else 5
    return "\n".join(count * [output])
有几件事:

实际上,您从未将
用户\u字符串
传递到
字母表
,而是传递硬编码字符串文字。在
字母表
中,您甚至不使用
字符串
参数。相反,您依赖于外部作用域中是否存在
user\u 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")

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

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循环
中,我们检查
所需字母
列表中的每个字母。如果
下字符串中没有出现一个
字母
,则返回6。如果循环退出,意味着没有返回任何值,这意味着letters\u Required列表中的每个字母都会在
下\u字符串中多次出现,因此我们可以返回5。最后,我们打印所需的字符串,在最后添加一个换行符
\n
,即
字母表
函数返回的次数。

为什么函数不使用
字符串
参数?
如果alph>='a'和alph您的标题说问题是检查是否所有字母都出现了。但问题是,问题只是返回了多个字符串,字符串之间有换行符。如果len(set(“abcdefghij”)。intersection(user_string.lower())==len(“abcdefghij”)
:打印5次,否则打印6次,那么您似乎接受了一个完全不能正确回答帖子的答案。或者更好:
set.issubset()
缺乏解释,对新用户来说有点棘手<代码>如果设置(“abcdefghij”).issubset(string.lower()):
。。。你也会同意吗