What am I missing here? /u/Clearhead09 Python Education

After my post yesterday about writing more concise code, I’ve tried my hand at writing as little code as possible that gets the job done and trying to break the problem down into smaller, logical steps to think through the problem a little easier and make it clearer what exactly needs to be done.

I am currently stuck on the MOOC problem “A Word Squared” in which a squared function is passed a string argument and an integer argument and prints out the following format:

squared("ab", 3) print() squared("abc", 5) aba bab aba abcab cabca bcabc abcab cabca 

Here is my code which works for the first problem (“ab”, 3) but for some reason the second problem (
“abc”, 5) doesn’t work as the last letter at the end of the first line abcab is the first letter used on the next line (b).

e.g.

abcab
bcabc
cabca
abcab
bcabc

Here is my code:

def squared( text , size ): i = 0 while i < size : row = text * size print(row[i:i + size ]) i += 1 # # You can test your function by calling it within the following block if __name__ == "__main__": squared("abc", 5) 

My thought process is that i = 0 in the first loop so the print statement prints from row[0:0 + size] which is the string “abcab”, which is correct, then the issue I am struggling with is the next iteration i = 1 so then it prints row[1:1 + size], which starts the loop at the letter “b”, which is the last character at the end of the first line printed, and so on.

I have thought about printing row[i + 1: i + size] to increase i and take the first character from before to the next character in the string (fixing the issue) but that just makes the string 4 characters long, also adding 1 to size to remedy the shorter string prints out the first 3 lines correctly, but the last two run into the first trouble.

Apologies if this seems like the rabblings of a madman but I am trying to get my thought process right early on, any guidance is greatly appreciated.

submitted by /u/Clearhead09
[link] [comments]

​r/learnpython After my post yesterday about writing more concise code, I’ve tried my hand at writing as little code as possible that gets the job done and trying to break the problem down into smaller, logical steps to think through the problem a little easier and make it clearer what exactly needs to be done. I am currently stuck on the MOOC problem “A Word Squared” in which a squared function is passed a string argument and an integer argument and prints out the following format: squared(“ab”, 3) print() squared(“abc”, 5) aba bab aba abcab cabca bcabc abcab cabca Here is my code which works for the first problem (“ab”, 3) but for some reason the second problem ( “abc”, 5) doesn’t work as the last letter at the end of the first line abcab is the first letter used on the next line (b). e.g. abcab bcabc cabca abcab bcabc Here is my code: def squared( text , size ): i = 0 while i < size : row = text * size print(row[i:i + size ]) i += 1 # # You can test your function by calling it within the following block if __name__ == “__main__”: squared(“abc”, 5) My thought process is that i = 0 in the first loop so the print statement prints from row[0:0 + size] which is the string “abcab”, which is correct, then the issue I am struggling with is the next iteration i = 1 so then it prints row[1:1 + size], which starts the loop at the letter “b”, which is the last character at the end of the first line printed, and so on. I have thought about printing row[i + 1: i + size] to increase i and take the first character from before to the next character in the string (fixing the issue) but that just makes the string 4 characters long, also adding 1 to size to remedy the shorter string prints out the first 3 lines correctly, but the last two run into the first trouble. Apologies if this seems like the rabblings of a madman but I am trying to get my thought process right early on, any guidance is greatly appreciated. submitted by /u/Clearhead09 [link] [comments] 

After my post yesterday about writing more concise code, I’ve tried my hand at writing as little code as possible that gets the job done and trying to break the problem down into smaller, logical steps to think through the problem a little easier and make it clearer what exactly needs to be done.

I am currently stuck on the MOOC problem “A Word Squared” in which a squared function is passed a string argument and an integer argument and prints out the following format:

squared("ab", 3) print() squared("abc", 5) aba bab aba abcab cabca bcabc abcab cabca 

Here is my code which works for the first problem (“ab”, 3) but for some reason the second problem (
“abc”, 5) doesn’t work as the last letter at the end of the first line abcab is the first letter used on the next line (b).

e.g.

abcab
bcabc
cabca
abcab
bcabc

Here is my code:

def squared( text , size ): i = 0 while i < size : row = text * size print(row[i:i + size ]) i += 1 # # You can test your function by calling it within the following block if __name__ == "__main__": squared("abc", 5) 

My thought process is that i = 0 in the first loop so the print statement prints from row[0:0 + size] which is the string “abcab”, which is correct, then the issue I am struggling with is the next iteration i = 1 so then it prints row[1:1 + size], which starts the loop at the letter “b”, which is the last character at the end of the first line printed, and so on.

I have thought about printing row[i + 1: i + size] to increase i and take the first character from before to the next character in the string (fixing the issue) but that just makes the string 4 characters long, also adding 1 to size to remedy the shorter string prints out the first 3 lines correctly, but the last two run into the first trouble.

Apologies if this seems like the rabblings of a madman but I am trying to get my thought process right early on, any guidance is greatly appreciated.

submitted by /u/Clearhead09
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *