I was using my old C++ assignments to teach myself Python and ran into issues every other line of code. In my attempt to find another way to ease into the lang I found what I think is an awesome tool "How to Think Like a (Python) Programmer" I understand most of what is being talked about and the exercises are simple, so I can learn the languge in smaller bites. Which, brings be to my issue.

The Exercise:
Write a function right_justify that takes a string named s as a parameter and that prints the string with enough leading space so that the last letter is in column 70 of the diplay

The Code:

def right_just(s):
    while len(s)!=70: # while statement to add white space to sting until lenght ==70
        " "+s
    print s  # print string with last letter in column 70 of the display

Most of the problems I have had so far are caused by thinking in C++ and not Python. I would not doubt that this is the problem here, but I can not find a good example to help me see my error. Thanx in advance I really appreciate all time people take to answer my questions.

I am also aware of rjust(), ljust(), and center() I am just trying to complete the exercise.


Re: How the while works in Python? 80 80

Really not that much different than C:

# while statement to add spaces to string until lenght == 70

def right_just(s):
    while len(s) < 70:
        s = " " + s
    return s

s = '1234567890'
# acts as ruler
print s * 7

print right_just(s)

my result -->
Re: How the while works in Python? 80 80


Now that I see your code I see I just made a bunch of rookie mistakes. Much Thanx


Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.