我是Python的新手,我有一个问题:
在下面的代码中,我需要向后迭代并打印字符串,通常是这样的:" deRRed"
# Mirror Color
# [ ] get user input, fav_color
# [ ] print fav_color backwards + fav_color
# example: "Red" prints "deRRed"
fav_color = input("What is your favorite color? ")
for letter in fav_color[::-1]:
print(letter,fav_color)
我可以打印,但我不能在一行上获得deRRed或neerGGreen。 如何让它在一行上打印?代码输出如下所示:
What is your favorite color? red
d red
e red
r red
我做错了什么?
答案 0 :(得分:0)
您需要将反向迭代字符串存储在其他变量中,之后:
fav_color=input("enter your favourite color ")
new=" "
for a in fav_color[::-1]:
new=new+a
print(new+fav_color)
答案 1 :(得分:0)
尝试
fav_color = input("what is your fav color?")
print(fav_color[::-1]+fav_color)
答案 2 :(得分:0)
您正在为反向字符串中的每个字母打印整个输入字符串。而是先打印后退字符串,然后再打印常规字符串:
print(fav_color[::-1] + color)
或者如果您需要逐个字符打印:
for letter in fav_color[::-1] + fav_color:
print(letter, end='')
答案 3 :(得分:-1)
fav_color = input("What is your favoutite color")
for letter in fav_color[::-1]:
print(fav_color[::-1] + fav_color)
输出如下:
你最喜欢的颜色是什么:绿色
neerGGreen
neerGGreen
neerGGreen
neerGGreen
neerGGreen