If you want to iterate from one number to another (not including these numbers), make a loop simple:
a = 10
b = 5
while a > b + 1:
a -= 1
print(a)
See here the code running
The while
continues running while a
is greater than b + 1
(the condition must be such that b
not be considered). Within the loop, i subtract 1 from a
and print its value (in that order, so I guarantee that the 10
will not be part of the sequence). The above code prints the numbers 9, 8, 7 and 6 in this order.
Notice that at first, you don’t need the if
. If a
is less than or equal to b + 1
, the code will not enter the while
, then the if
is redundant. It would only be necessary if you wanted to take some action if a
is less than or equal to b + 1
. Ex:
if a > b + 1:
while a > b + 1:
a -= 1
print(a)
else:
print('O valor de a deve ser maior que b + 1')
Then it makes sense to have the if
. Otherwise, it’s just noise. Note that the condition is if a > b + 1
, because if it were only if a > b
, could fall in the event that a
is 10 and b
is 9, in which case no number will be printed (therefore it should also fall on the else
).
There is also no reason to create this variable cont
. OK, maybe it was necessary if you did not want to change the value of a
, but within the loop you change the value of a
and of cont
. Then you can use only the variable a
.
But if you really want to use another variable (maybe because the value of a
cannot be changed, for example), you could initialize it with the first value of the sequence and do the loop thus:
a = 10
b = 5
n = a - 1
while n > b:
print(n)
n -= 1
See here the code running
There are other details that have changed: now the condition is n > b
(and not b + 1
), and within the loop i print the value and then subtract 1. Note that it is not necessary to subtract 1 from a
, because it is not used inside the loop and there is no reason to change its value.
I suggest you do the table test in both codes, to better understand how they work (because in one the condition is b + 1
and the next it’s just b
, for example).
From what I’ve seen in the comments, this is an exercise, so it’s very likely they want you to do a loop like the one above.
But in Python you can also use a range
, which represents precisely a sequence of numbers:
a = 10
b = 5
for i in range(a - 1, b, -1):
print(i)
See here the code running
The first parameter is the initial value (in the case, a - 1
, since the a
cannot be part of the sequence).
The second parameter is the final value, but I put b
, for in a range
the initial value is included, but the final value is not. So it is already guaranteed that b
will not be part of the sequence.
The third parameter is the "step". In this case, it is -1
, since the sequence begins in a - 1
and goes "walking" from -1 to -1, until you get to b
. The above code also prints the numbers 9, 8, 7 and 6, in this order.
And in that case you don’t need if
, for if a
is less than b
, the range
will have no number (example). The if
only makes sense if you want to take any specific action if the numbers are invalid (like the example above, which shows a message saying that a
must be bigger than b + 1
).
I recommend that you research how to use a Debugger with python, this way you can analyze the flow of your program step by step and who knows what is going wrong. I don’t know which editor you’re using, but one of the easiest ways to debug python is to use the Vscode editor by clicking on the gear. From the original python interface (idle) you will not be able to debug...
– wkrueger
What is the expected result? What was the obtained result? Why the value of
a
changes during the loop?– Woss
That one
if
is strange. If you’ve checkeda >= b
, don’t need to checkb <= a
, is redundant. The condition ofwhile
is also strange, becausecont
starts with the same value asa
and within the loop both are decremented, socont <= a
will always be true.– hkotsubo
Gabriel - even though the program is yours, and you snedo the only user, a
input()
with nothing and you have to guess that will type a larger number and then a smaller number with nothing on the screen is something really bad to use. IN Python, theinput
already accepts a string that can print instructions on what is being typed - I suggest you use, and learn to value the "user experience" - even if the user is just you. Continuing - if you put some "prints" on the "Else" of the "if", and others inside, you will understand better what you are doing there.– jsbueno
Thanks guys for your help. first wkrueger, I used debug and saw the step by step algorithm only that for example I put in the values in the variable "a" = (10) and in the variable "b" = (5), the result comes out 9,8,7,6,5 only that the expected result would be the open interval ]A.. B[.
– Gabriel
thanks guys for the help. according to Anderson Carlos Woss, then the expected result would be the open interval ]A.. B[ for example: variable "a" = (10) and variable "b" = (5), ai would like to have this result 9,8,7,6. and the result was 9,8,7,6,5 and the input value of variable "b" is being displayed. and why the value of "a" changes is because it was the only way I could get it to show in countdown a = a - 1.
– Gabriel
thanks guys for the help. third hkotsubo, the worst is that I know this guy... But it was only with this condition that I almost arrived at the expected answer, as I was left with no option to ask you why I thought it was a simple program and it turned out to be very complicated. But thanks for the help, I will try to improve if and while conditions.
– Gabriel
Thanks guys for your help. jsbueno room, thanks guy I will improve the code for better visualization, I did not put before because this is a program for college and they ask for nothing to be put in the input, then I thought it was good so, for thinking that the program would be easy. But I’ll improve the code. (It’s just that I’m starting now)
– Gabriel