0
Spelling Backwards
I need help on the spelling backwards challenge.
11 Antworten
+ 9
def spell(txt):
#your code goes here
if txt=="":
return
else:
print(txt[-1])
return spell(txt[:-1])
txt = input()
spell(txt)
+ 4
def spell(txt):
#your code goes here
if txt=="":
return txt
else:
print(txt[len(txt)-1])
return spell(txt[0:len(txt)-1])
txt = input()
print(spell(txt))
+ 3
Ilyas Guissous ,
please also put your code in playground, save it and post the link to this file here.
thanks!
+ 2
Use [::-1]
Example:
Str = str(input())
print(Str[::-1])
*******************************
There is another way:
Str = str(input())
List = list(Str)
List_reverse = List[::-1]
for i in List_reverse:
print(i)
+ 2
def spell(txt):
#your code goes here
x=len(txt) #x is length of string
if x==0:
return txt
else:
print(txt[-1])
return spell(txt[0:x-1]) #colon operater is important to slice the string
txt = input()
spell(txt)
+ 1
Language??
And what to do? (elaborate it)
+ 1
it seems to be the *intermediate python* tutorial exercise 19. the task description is:
Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.
Sample Input
HELLO
Sample Output
O
L
L
E
H
Complete the recursive spell() function to produce the expected result
+ 1
Beh ,
your codes are not both absolutely correct, since the first one outputs the string in one line.
but following the task description this task should be done with a recursive function.
0
def spell(txt):
#your code goes here
if len(txt) == 0:
return 0
else:
for ch in range(len(txt),0,-1):
ch-=1
print(txt[ch])
txt = input()
spell(txt)
#For some reason they added a new line for each letter in the input. Because they don't wanna make it easy.
0
Thanks everyone!