这是一个程序,我必须在其中实现一个函数,该函数将检查从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行?
我猜你说的
alphabet
是指letter
。鉴于此,代码应该如下所示:说明
在
for loop
中,我们浏览letters_needed
列表中的每个字母。如果一个letter
没有出现在lower_string
中,则返回6。如果循环退出,意味着没有返回任何值,这意味着letters_Required列表中的每个字母都会在lower_string
中多次出现,因此我们可以返回5。最后,我们打印所需的字符串,最后添加一个换行符\n
,即alphabets
函数返回的次数有几件事:
实际上,您从未将
user_string
传递给alphabet
,而是传递一个硬编码的字符串文本。在alphabet
中,您甚至不使用string
参数。相反,您依赖于外部作用域中user_string
的存在要在单独的行上打印五个或六个字符串,可以
str.join
使用换行符打印字符串列表,如下所示:。。。或者在每行中包含一个换行符,然后将消息字符串“相乘”五到六次——在本例中,您需要将
print
的可选end
参数设置为空字符串,如下所述您还可以显示/解压缩消息列表,并将
print
的可选sep
参数设置为换行符:您只能使用以下方法获取字符串的唯一字符:
例如
给出:
显然,首先要通过小写函数传递它
编辑对不起,我没有完全阅读这个问题,我更关心的是您的计数实施情况
您可以使用以下命令在不同的行上输出N次:
相关问题 更多 >
编程相关推荐