我是编码新手,上个月开始学习python。我的朋友给了我这个问题,我被卡住了
def gordon(a):
x = a.split()
result = " "
for y in x:
c = " ".join((y.upper().replace("A", "@").replace("E", "*").replace("I", "*").replace("O", "*").replace("U", "*"))) + "!!!!"
gordon('What feck damn cake')
我想返回的值
W H @ T!!!! F * C K!!!! D @ M N!!!! C @ K *!!!!
我在网上收到的东西,我找不到修复它的方法
W H @ T!!!!
F * C K!!!!
D @ M N!!!!
C @ K *!!!!
您必须在循环中打印c。尝试c+=,而不是只使用c=打印c循环外
def gordon(a):
x = a.split()
result = []
for y in x:
result.append(
y
.upper()
.replace("A", "@")
.replace("E", "*")
.replace("I", "*")
.replace("O", "*")
.replace("U", "*")
+ "!!!!"
)
return " ".join(result)
gordon('What feck damn cake')
为了给您的工作提供非常接近的答案,这里是对代码的小修复,但最终会导致额外的空间。
def gordon(a):
x = a.split()
result = ""
for y in x:
result += " ".join((y.upper().replace("A", "@").replace("E", "*").replace("I", "*").replace("O", "*").replace("U", "*"))) + "!!!! "
print(result)
gordon('What feck damn cake')
对于更好的,我们应该使用";结果";作为阵列来收集每个";c";并用一个空格("(连接所有的c。
def gordon(a):
x = a.split()
result = []
for y in x:
c = " ".join((y.upper().replace("A", "@").replace("E", "*").replace("I", "*").replace("O", "*").replace("U", "*"))) + "!!!!"
result.append(c)
print(" ".join(result))
gordon('What feck damn cake')