0

I have to write a program that gets multiple strings one after the other that is terminated by the sentinel DONE, and then print this list of strings right justified to the length of the biggest string. Here is my code:

user = input("Enter strings (end with DONE):\n")
totalString = ""

while True:
    if user.lower() == 'done':
        break
    else:
        totalString = totalString + user + " "
        user = input("")

lst = totalString.split(" ")
max = 0

for i in range(len(lst)):
    if len(lst[i]) > max:
        max = len(lst[i])

for i in range(len(lst)):
    print("{:>{derp}}".format(lst[i],derp=max))

The issue I'm having is that the if statement in the while loop never executes, so it just gets stuck in that loop.

Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
  • 2
    Try printing the value of `user` before the `while` loop, because it might have a `\n` at it's end. Use `user=user.strip()` to fix that. It works fine on my system. – Ashwini Chaudhary Apr 23 '13 at 15:45
  • 1
    I've added the python3x tag, coz you're using `input()` to get string from the user. – Ashwini Chaudhary Apr 23 '13 at 15:49
  • @AshwiniChaudhary How does that follow? [`input()`](http://docs.python.org/2/library/functions.html#input) exists in `2.X`... Oh, it's the print(). Nevermind. – thegrinner Apr 23 '13 at 15:50
  • @thegrinner yes but in py2x input acts as `eval(raw_input())`, so you can't use it to get string from the user. – Ashwini Chaudhary Apr 23 '13 at 15:52
  • 1
    Few more things: 1) use `while 1` instead of `while True` - in Python True is just a variable which value can be changed to anything, so it requires extra check for the `while` loop 2) use `if 'done' in user.lower():` - this is more Pythonic way 3) unless you need the result of `range(len(lst))` use `xrange` instead - which is faster and uses less memory – SpankMe Apr 23 '13 at 15:53
  • Yeah it is python 3.2, sorry for that guys – Blackbeard Apr 23 '13 at 15:53
  • @AshwiniChaudhary That's why I was worried about adding the tag if it wasn't 3.2. – thegrinner Apr 23 '13 at 15:53
  • @SpankMe `if 'done' in user.lower()` will be true for `"iamdonetoo"`as well. – Ashwini Chaudhary Apr 23 '13 at 15:58
  • @user2312022 try using a `.strip` in front of `input()` : `user = input("").strip()` – Ashwini Chaudhary Apr 23 '13 at 16:07
  • @AshwiniChaudhary restarting my computer and adding in the user = user.strip() worked – Blackbeard Apr 23 '13 at 16:08
  • @user2312022 restarting wasn't required here :). I suspect you were using cmd to run this code, which adds a newline in front of the input. And can be removed using [`strip`](http://docs.python.org/2/library/stdtypes.html#str.strip). – Ashwini Chaudhary Apr 23 '13 at 16:11
  • I was indeed, much appreciated :) – Blackbeard Apr 23 '13 at 16:14
  • Note that this was a bug for the Windows port of Python 3.2.0 that has since been fixed. (http://bugs.python.org/issue11272) If you upgrade your Python installation to 3.2.4 or 3.3.1, you don't need Ashwini Chaudhary's solution any more. – Jasmijn Apr 24 '13 at 21:10

2 Answers2

0

NB: Assumes that code was for Python 2.x, which might not be a case.

Firstly with input(), you are expecting a numeric value and not a string. Just changing your input() to raw_input() did the trick for me. As pointed in the comment, maybe the OP is using Python 3.

This question on SO explains the differences in Python 2.x and 3.x wrt input() and raw_input().

Community
  • 1
  • 1
Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
0

As you're using cmd to run your code so the string returned by input contains a return character(\r) as well.

So when the user enters "done", input() actually returns "done\r".

A simple solution is to use str.strip here:

user = input("").strip("\r")

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504