Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Simran Bansari
4,517 PointsI need help making a python program that which gets a phrase from a user and reverses it. This is what I have so far.
string = str(input("Put a word or sentence that you would like to reverse !!") def reverse(str): return str[::-1]
7 Answers
Umesh Ravji
42,386 PointsHi Simran, I changed input() to raw_input() so strings didn't have to be inside quotes, what is the problem? The code appears to run fine?
def reverse(str):
return str[::-1]
string = str(raw_input("Put a word or sentence that you would like to reverse !! "))
print reverse(string)
Put a word or sentence that you would like to reverse !! monkey
yeknom
Maybe you are defining your function after you ask for the string?
Simran Bansari
4,517 Pointsfor the 4rth line of code where it says print reverse(string), i get a syntax error and when I tried fixing it by changing it to print(reverse(string)), I still got a invalid syntax error pointing to the print function
Umesh Ravji
42,386 PointsIs this for a challenge or is this for your own?
Simran Bansari
4,517 PointsIt is for me
Cole Wilson
7,413 PointsHi Simran,
I was wondering if you just wanted to reverse the order that the words appear. Here is an example that will split the string into a list using a space as the delimiter. It then reverses the order of the list and joins it back into a string with a space inserted between each element of the list.
As an example, the sentence "I love Python so much" would be returned as "much so Python love I".
def reverse_phrase(phrase):
return ' '.join(phrase.split(' ')[::-1])
user_phrase = raw_input('Please supply a phrase to reverse. \n')
print(reverse_phrase(user_phrase))
Simran Bansari
4,517 Pointsi more like wanted a program that which if given "hello" it would output "olleh"
Simran Bansari
4,517 Pointsbut I have the code down now, my only problem is the syntax error I am getting for the print function in the last line which is print(reverse(string)) , do you know perhaps why there is a syntax error pointing to print??
Simran Bansari
4,517 Points//This is my code//
def reverse(str): return str[::-1]
string = str(input("Put a phrase you want reversed!!")
print(reverse(string))
Cole Wilson
7,413 PointsAs Umesh also pointed about above, perhaps the issue is input versus raw_input. If you are using python 2.x then input will not return as a string you will get an error.
Otherwise your code looks correct to me.
You can try two things:
- use the " around the word when using your current code.
- try raw_input instead of input.
Simran Bansari
4,517 Points//This is how my code looks but I am still getting a syntax error pointing to print??
def reverse(str): return str[::-1]
string = str(raw_input("Put a word or sentence that you would like to reverse !! "))
print reverse(string)
Cole Wilson
7,413 PointsIn your function try replacing the str variable with something else like my_str. I think there might be an issue with the built in str class and your repurposing it within your function.
def reverse(my_str): return my_str[::-1]
Simran Bansari
4,517 Points//This is my code now but I am still getting the syntax error pointing to print??//
def reverse(str): return str[::-1] string = str(raw_input("Put a phrase you want reversed!!")
print reverse(string)
Cole Wilson
7,413 PointsI think you need to change the name of the parameter in your reverse function to something other than str. The keyword str is already used by Python.