Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中解开一个非常简单的代码_Python_Python 3.x_Decode_Lowercase - Fatal编程技术网

在python中解开一个非常简单的代码

在python中解开一个非常简单的代码,python,python-3.x,decode,lowercase,Python,Python 3.x,Decode,Lowercase,以下是简要说明: 我需要输入如下内容: BaSe fOO ThE AttAcK 并返回: attack the base. 正如您所看到的,要解码这个,我需要以相反的顺序读取单词,如果单词的第一个字母是upper(),则将其设置为小写,并将其附加到我稍后将打印的列表中。这就是我到目前为止所做的: # Enter your code for "BaSe fOO ThE AttAcK" here. default = input('code: ') listdefault = default.s

以下是简要说明: 我需要输入如下内容:

BaSe fOO ThE AttAcK
并返回:

attack the base.
正如您所看到的,要解码这个,我需要以相反的顺序读取单词,如果单词的第一个字母是upper(),则将其设置为小写,并将其附加到我稍后将打印的列表中。这就是我到目前为止所做的:

# Enter your code for "BaSe fOO ThE AttAcK" here.
default = input('code: ')
listdefault = default.split()
uncrypted = []
for i in range(len(listdefault)):
  if listdefault[:-i].istitle(): # doesn't work
    i = i.lower() # dont know if this works, Should convert word to lower case.
    uncrypted.append(i)
solution = ' '.join(uncrypted)
print(solution)

有人能告诉我如何让这个程序工作吗?表示我不能对列表类型使用istitle()方法。

您很接近,但您正在将
i
视为索引(
,用于范围内的i…
)和单词本身(
uncrypted.append(i)
)。您可能指的是
listdefault[i].istitle()
,而不是
listdefault[:-i].istitle()

最好的解决方案是将其更改为:

for w in listdefault[::-1]:
    if w.istitle():
        w = w.lower()
        uncrypted.append(w)
因为
listdefault[::-1]
是一种反转列表的方法(
reversed(listdefault)
也可以)。如果您知道如何使用列表理解,您可以在一行中完成:

solution = ' '.join([w.lower() for w in listdefault[::-1] if w.istitle()])

很接近,但您正在将
i
视为索引(
表示范围内的i…
)和单词本身(
uncrypted.append(i)
)之间切换。您可能指的是
listdefault[i].istitle()
,而不是
listdefault[:-i].istitle()

最好的解决方案是将其更改为:

for w in listdefault[::-1]:
    if w.istitle():
        w = w.lower()
        uncrypted.append(w)
因为
listdefault[::-1]
是一种反转列表的方法(
reversed(listdefault)
也可以)。如果您知道如何使用列表理解,您可以在一行中完成:

solution = ' '.join([w.lower() for w in listdefault[::-1] if w.istitle()])

这可以做得简单得多

text = input("Code: ")
result = [w.lower() for w in reversed(text.split()) if w[0].isupper()]
print(' '.join(result))
我希望这能让您更深入地学习Python


顺便说一下,您不能使用
.istitle()
执行此任务,因为它会检查是否只有第一个字母是大写。

这可以简单得多

text = input("Code: ")
result = [w.lower() for w in reversed(text.split()) if w[0].isupper()]
print(' '.join(result))
我希望这能让您更深入地学习Python


顺便说一下,您不能将
.istitle()
用于此任务,因为它会检查是否只有第一个字母是大写。

这是一个合理的解决方案:

default = input('code: ')
listdefault = default.split()
uncrypted = []
for i in reversed(listdefault):
    if i[0].isupper():
        i = i.lower()
        uncrypted.append(i)
solution = ' '.join(uncrypted)
print('says:',solution)

这是一个合理的解决方案:

default = input('code: ')
listdefault = default.split()
uncrypted = []
for i in reversed(listdefault):
    if i[0].isupper():
        i = i.lower()
        uncrypted.append(i)
solution = ' '.join(uncrypted)
print('says:',solution)

感谢您的全面回复!感谢您的全面回复!