+ 1
what should I do to print numbers 5 6 7 instead of 5 7
s ="1,3,5-7,9,10-13" o/p 1 3 5 6 7 9 10 11 12 13 #code s = '1,3,5-7,9,10-13' for i in s: if i == '-' or i == ',': pass else: print(i,end='\n')
5 Respuestas
+ 5
( 1 )
First, when iterating the string, iterating 2-digit numbers is not possible because it consist of two characters, for example, 10. When we the iteration happens, it will not iterate '10' but instead it will iterate '1' and '0' separately.
To solve this, we used string.split(',') method and splits the string and convert into list by Comma (,) so that every number is iterated.
( 2 )
Since we have split the string with comma, the condition if i == ',' is no longer needed.
Then the condition will now be:
---> if '-' in i:
This checks if there is a dash in the number, If there is one:
For example: 5-7
Then it will create an iterator (5 to 7). But to get the value of 5 and 7, we should get first the index of the dash and use it to index numbers that will be used as the 'start' and 'end(+1)' in range.
If you have more questions, please feel free to ask. Thanks!
https://code.sololearn.com/cA11a20a20a1
+ 2
《 Nicko12 》 thanks for your reply........
I want to print 5-7 like
5
6
7
and for 10-13
10
11
12
13
I think we should start a loop for that
+ 2
Sorry I just forgot to add +1 to the 'end' of the range, try to run it again, I've edited it. Thanks for the update.
+ 2
《 Nicko12 》 yeah it's perfect......mean while I try and got results https://code.sololearn.com/cn83ezFOzaGw/?ref=app
Thanks alot bro