-2

So I'm trying to reverse an output in Python but I don't know how to. I tried looking up solutions online, but they haven't worked at all. Here's the piece of code I need to reverse (particularly the last line).

# FIXME (2): Output the four values in reverse

user_integer = int(input('Enter integer (32 - 126):\n'))
user_float = float(input('Enter float:\n'))
user_character = input('Enter character:\n')
user_string = input('Enter string:\n')
print(user_integer, user_float, user_character, user_string, '\n')
Kryptix
  • 11
  • 1
  • Reverse what, exactly? Given your current code, just manually reverse the arguments to `print` – roganjosh May 25 '20 at 00:26
  • @roganjosh This is the code I need reversed: `print(user_integer, user_float, user_character, user_string, '\n')` – Kryptix May 25 '20 at 00:30

2 Answers2

-1

You can reverse any iterable that supports slices like so:

Python 3.8.0 (default, Dec  3 2019, 17:33:19)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> mystring = "foobarbaz"
>>> mystring[::-1]
zabraboof
>>>

There is an explanation of slice notation here.

Edit: If the iterable does not support slices then you can still use the reversed built-in. For simplicity I will demonstrate with a string, although you will usually want to use the method I described above when manipulating strings.

Python 3.8.0 (default, Dec  3 2019, 17:33:19)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> mystring = "foobarbaz"
>>> ''.join(reversed(mystring))
'zabraboof'
>>>
causaSui
  • 166
  • 9
  • This doesn't answer the OP's question because they have multiple arguments going to `print` – roganjosh May 25 '20 at 00:33
  • To be honest, since I'm not sure exactly what is needed, I was answering the question as asked in the title. I agree with your comments that the OP's problem statement is not well formed enough to give me any special confidence that this will or will not solve their problem. – causaSui May 25 '20 at 00:37
-1

three years later and I'm going to just post the full answer to the question because I took the same course. This is for all future students, you're welcome!

# FIXME (1): Finish reading other items into variables, then output the four values on a single line separated by a space
user_int = int(input('Enter integer (32 - 126):\n'))
user_flo = float(input('Enter float:\n'))
user_cha = input('Enter character:\n')
user_str = input('Enter string:\n')  

print(user_int, user_flo, user_cha, user_str)



# FIXME (2): Output the four values in reverse

print(user_str, user_cha, user_flo, user_int)



# FIXME (3): Convert the integer to a character, and output that character

print(user_int, 'converted to a character is', chr(user_int))