-1

My current code is:

for i in range(len(phoneList)):
    print("|{}|{}|{}|{}|".format(formattedPhoneList[i],namesList[i],number[i], lastDue[i]))
    if float(lastDue[i]) > 850:
        print("**")
    elif float(lastDue[i]) < 850 and int(number[i]) > 350:
        print("++")
    else:
        continue

Desired Output would be:

|Index[0] of formattedPhoneList|Name Index[0]|384|$ 976.97|**
|Index[1] of formattedPhoneList|Name Index[1]|132|$ 188.81|
|Index[2] of formattedPhoneList|Name Index[2]|363|$ 827.48|++

Current Output:

|Index[0] of formattedPhoneList|Name Index[0]|384|$ 976.97|
**
|Index[1] of formattedPhoneList|Name Index[1]|132|$ 188.81|
|Index[2] of formattedPhoneList|Name Index[2]|363|$ 827.48|
++

I have tried inserting a trailing comma and import stdout with no success. Is there another way to format this? Thank you for reading.

Bucciarati
  • 17
  • 5
  • @Alik I've tried using their stdout method but I believe this issue is different because of the format before the if statement. Thanks for the link though! – Bucciarati Jan 22 '16 at 08:44
  • `format` is a string function. It doesn't affect `print` in any way. Try to use `end` parameter of `print` – Konstantin Jan 22 '16 at 08:46
  • @Alik Sorry, I'm still quite new, where would I use ends parameter of print? – Bucciarati Jan 22 '16 at 08:47
  • add it to the first call of `print` – Konstantin Jan 22 '16 at 08:49
  • I've tried doing this print("|{}|{}|{}|{}|".format(formattedPhoneList[i],namesList[i],number[i], lastDue[i]), end="") but the output format is randomly aligned with the second line having many spaces inbetween and the third line attaching itself to the second line. – Bucciarati Jan 22 '16 at 08:54

2 Answers2

1

I think the simplest solution is to do this:

for i in range(len(phoneList)):
    if float(lastDue[i]) > 850:
        extra = "**"
    elif float(lastDue[i]) < 850 and int(number[i]) > 350:
        extra = "++"
    else:
        extra = ""
    print("|{}|{}|{}|{}|{}".format(formattedPhoneList[i],namesList[i],number[i], lastDue[i], extra))
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • This works splendid. Thank you! – Bucciarati Jan 22 '16 at 08:57
  • I noticed that it will skip the special cases if `lastDue[i]` is exactly `850`. Perhaps you might want to change the first comparison to `>=` or the second to `<=` ? I don't know enough about the intent to know which makes the most sense. – Tom Karzes Jan 22 '16 at 09:02
0

You could use a variable

for i in range(len(phoneList)):
    data = "|{}|{}|{}|{}|".format(formattedPhoneList[i],namesList[i],number[i], lastDue[i]))
    if float(lastDue[i]) > 850:
        data += " **"
    elif float(lastDue[i]) < 850 and int(number[i]) > 350:
        data += " ++"
    print data
Clement Puech
  • 115
  • 1
  • 9