febeks17 febeks17 - 1 month ago 5
Python Question

Loop through user input in range

I need to loop through an user input (regular text message with letters, numbers and other regular characters including whitespaces) in a range, for example:

My range is read by 3 characters from position (or index) 3:

-m
is just a
switch
for console so it knows that it is a text message, so I want to ignore the -m + following white space and start considering the string and its size from H. So the string size will be 12 (5 + 1 + 6).

My attempt:

message = "-m Hello World!"

fragSize = 3
fragCount =0
start = 0
end = fragSize


if message.startswith('-m '):
message = message[3:] #separates -m from the message

for index in range(1, len(message)):
print('index[start:end]')
start = end
end += fragSize


Message:

-m Hello world!


Output should be:

Hel
lo_ (_ is a whitespace)
wor
ld!

Answer
message = '-m Hello world!'
if message.startswith('-m '):
    message = message[3:]
while message!= '':
    print message[:3]
    message = message[3:]

Hel
lo
wor
ld!

Comments