Hi, everybody.
Why do I have to duplicate the variable 'k' in the code below?
I tried NOT doing it, but the counter doesn't work properly unless I create the variable 'counter' to receive 'k'.
(The script is supposed to receive the inputs 'initial number', 'final number', 'step' and then print the whole progression).
Thanks in advance!
Well, since I can't edit/delete my post, there goes the code again, now revised...
Why do I have to duplicate the variable 'k' in the code below?
I tried NOT doing it, but the counter doesn't work properly unless I create the variable 'counter' to receive 'k'.
(The script is supposed to receive the inputs 'initial number', 'final number', 'step' and then print the whole progression).
Thanks in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
def counter(k, y, z): from time import sleep if z < 0 : z * = - z if k < y: count = k ## WHY?? WHEN I IGNORE THIS AND USE K ALONG THE LOOP, IT USES AN EXTRA (WRONG) VALUE IN THE END. while cont < = y: print ( f '{count}' , end = ' - ' if count + z < = y else '') count + = z sleep( 0.333 ) while k > = y: print (k, end = ' - ' if k - z > = y else '') k - = z sleep( 0.333 ) while True : start = int ( input ( 'Initial number: ' )) end = int ( input ( 'Final number: ' )) step = int ( input ( 'Step: ' )) counter(start, end, step) flag = str ( input ( '\nAgain? (S/N) ' )).strip().lower()[ 0 ] if flag = = 'n' : break |
Well, since I can't edit/delete my post, there goes the code again, now revised...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# encoding:utf-8 def counter(k, y, z): from time import sleep if z < 0 : z * = - z if k < y: count = k ## WHY DOESN'T IT WORK IF A SIMPLY USE K?? while count < = y: print (count, end = ' - ' if count + z < = y else '') count + = z sleep( 0.333 ) while k > = y: print (k, end = ' - ' if k - z > = y else '') k - = z sleep( 0.333 ) while True : initial = int ( input ( 'Initial number: ' )) final = int ( input ( 'Final number: ' )) step = int ( input ( 'Step: ' )) counter(initial, final, step) flag = str ( input ( '\nAgain? (Y/N) ' )).strip().lower()[ 0 ] if flag = = 'n' : break |