在对抗天网的战争中,人类试图在计算机意识到正在发生的事情的情况下相互传递信息。
为了做到这一点,他们使用了一个简单的代码:
他们以相反的顺序阅读单词,他们只注意以大写字母开头的信息中的单词,比如:
BaSe fOO ThE AttAcK包含以下消息:
攻击基地
然而,计算机已经捕获了你,迫使你写一个程序,这样他们就可以理解所有的人类信息(我们不会进入你经历过的可怕的折磨)。您的程序必须按照以下方式工作:
soMe SuPPLies liKE冰淇淋aRe iMPORtant oNly tO THeir cReaTORStO DestroY thEm iS pOInTLess.
代码: soMe SuPPLies liKE冰淇淋aRe iMPORtant oNly tO THeir cReaTORS.tO DestroY thEm iS pOInTLess.
上面写着:毁掉他们的冰淇淋用品
注意,除了提取消息外,我们还将每个单词小写,这样就更容易阅读了。
你能帮我查一下密码吗?到目前为止,这是我的代码:
output=[]
b=0
d=0
code=input("code: ")
code=code.split()
print(code)
a=len(code)
print(a)
while b<a:
c=code[b]
if c.isupper:
output.append(c)
b=b+1
elif c.islower:
b=b+1
else:
b=b+1
print(output)我需要最后一行说"BaSe the AttAck“,去掉"fOO”,在最后一步中,我将反转字符串,这样才有意义,但这并不是区分小写单词和大写单词。
发布于 2020-04-22 09:49:24
我重写了你的代码。
#code=input("code: ")
code = "soMe SuPPLies liKE Ice-cREAm aRe iMPORtant oNly tO THeir cReaTORS. tO DestroY thEm iS pOInTLess"
code=code.split()
output = []
for word in reversed(code): #iterate over the list in reverse
if word[0].isupper(): #check if the FIRST letter (word[0]) is uppercase.
output.append(word.lower()) #append word in lowercase to list.
output = " ".join(output) #join the elements of the list together in a string seperated by a space " "
print(output)输出
destroy their ice-cream supplies发布于 2021-02-08 00:20:23
这是我的答案,在grok学习和绿色的全面测试:
code = input('code: ')
code=code.split()
output = []
for word in reversed(code):
if word[0].isupper():
output.append(word.lower())
output = " ".join(output)
print('says:', output)
发布于 2020-04-22 09:57:59
您的代码有两个问题:
isupper和islower是方法,也就是说,您需要通过编写().c.isupper()来调用它们,以检查整个单词是否大写。然而,您的问题描述只需考虑每个单词的第一个字符。因此,尝试使用c[0].isupper().现在,在修复了它之后,您仍然需要倒转output列表(并使每个单词小写),但我想您还没有做到这一点。:-)
https://stackoverflow.com/questions/61361815
复制相似问题