- 1

What is wrong in this code I could not get right all the problems...

You are given a word and want to split it into even parts based on a number that is provided, each half being the size of the number. Task: Write a program that takes in a string, and a number as input. Split the string into even parts sized by the number, and output the parts separated by hyphens. The last part of the output will be any leftover, as the input string might not split into the provided parts evenly. Input Format: Two inputs: a string and an integer. Output Format: A string, representing the hyphen-separated parts. Sample Input: hello 2 Sample Output: he-ll-o Code string = list(input()) number = int(input()) #print(string) for i in range(number,len(string),number+1): string.insert(i,"-") for i in string: print(i,end="")

16th Jul 2021, 11:00 AM
Bitu Kumar Sha
Bitu Kumar Sha - avatar
2 ответов
0
Your number will not change, use i + number + 1 instead number + 1
22nd Jul 2021, 2:20 AM
Tegar
Tegar - avatar
0
I check with loop for, the code goes wrong, because the parameter number 3 in for loop cant go higher, so i think use while will be better i = number while i<len(string): string.insert(i,'-') i=i+number+1
22nd Jul 2021, 2:54 AM
Tegar
Tegar - avatar