Reputation: 23
Ok so I am trying to input "Hello World!" and I want the output to be "dlroW olleH" My code prints the opposite one by one. How do I fix this?
class Stack:
def __init__(self):
self.__items = []
def push(self, item):
self.__items.append(item)
def pop(self):
return self.__items.pop()
def peek(self):
return self.__items[len(self.__items)-1]
def is_empty(self):
return len(self.__items) == 0
def size(self):
return len(self.__items)
def __len__(self):
return len(self.__items)
x = Stack()
userinput = input("enter ")
for letter in userinput:
x.push(letter)
while x:
print(x.pop())
Upvotes: 0
Views: 43
Reputation: 34813
By default, every call to print()
writes to a new line on the screen.
You can change this with the end
argument to print()
, like so:
print(x.pop(), end='')
Then, all of the characters in the string will be printed all on the same line.
You can also add a plain call to print()
at the end to add a final end-of-line.
Upvotes: 1