0
A time limit error happened when i used the int i directly for the while loop why did using it directly cause such ?
The aim :- find and print numbers between a and b that are made up only of the digits 4 and 7 if nothing is in the range print -1 , a and b must be positive int The approach that did not work :- https://code.sololearn.com/cafuOJ5zT9vr/?ref=app The same approach but using temp works https://code.sololearn.com/cz29Pa3oT7Cv/?ref=app
2 odpowiedzi
+ 2
Dareen Moughrabi the first program has a bug because the inner while loop modifies i, which is outer for loop index. The outer loop dutifully continues incrementing and comparing the modified value. It never reaches the end value of b because the while loop keeps resetting i to a smaller value.
If you learned Python as your first language, then you will discover that Python for loops are different from other languages. In Python, the loop index gets replaced with the next value in the iterator each time through the loop. That allows you to modify the loop index with little consequence. It is not done that way in most other languages. Other languages simply update whatever value is currently in the loop index to the next increment and then check the end condition.
+ 1
Brian
Thx it really sliped over my mind that when i change the value of i i am also changing it in the for loop , not only in the while loop ,somehow i viewed them separately ,so i could not understand what was going on .