Print the open range between two numbers in descending order

Asked

Viewed 1,157 times

2

In the code below, I can make it not show variable value a, but the variable b ends up in the print.

The expected response would be the open interval ]A..B[.

Ex: variable a = 10 and variable b = 5.

Expected response: 9,8,7,6

Response obtained: 9,8,7,6,5. Whereas the values reported in input to be displayed, the variable value a I can make it so that it is not displayed on print, but the value of b always ends up leaving.

a = int(input('Valor de a tem que ser maior do que b: '))
b = int(input('Valor de b tem que ser menor do que a: '))

cont = a #Estou fazendo que a var cont receba o valor de a

if a > b and b < a: # O if estou verificando se a é maior do que b e b é menor que a
    while cont > b: # condição para o while é enquanto contador for maior que b vai repetir
        cont = cont - 1 # estou fazendo para que cont receba cont -1
        print(cont) # exibindo cont na tela
        a = a - 1 # estou fazendo que a receba a - 1 para que no cont fique com o valor de a, e depois ele subtraia - 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...

  • What is the expected result? What was the obtained result? Why the value of a changes during the loop?

  • That one if is strange. If you’ve checked a >= b, don’t need to check b <= a, is redundant. The condition of while is also strange, because cont starts with the same value as a and within the loop both are decremented, so cont <= a will always be true.

  • 1

    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, the input 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.

  • 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[.

  • 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.

  • 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.

  • 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)

Show 3 more comments

2 answers

4


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).

  • Thank you so much guy, you don’t know how much you’ve helped me.

  • Thank you so much guy, you don’t know how much you helped me.

  • I understand, thanks for the explanation was very detailed I will pay more attention to my conditions from now on. (and I accepted his reply.) It’s just that I wanted to accept the two answers yours and Jean’s which is just below, but it doesn’t let me.

  • @Gabriel Yeah, the system only lets you accept one answer. But when you have more than 15 points, may vote in all the answers you found useful

  • Got it, thanks again. And I’ll try to earn those 15 points to also accept Jean’s reply.

  • @Gabriel Just to be clear, accepting and voting are different things. You can only accept one answer (from a question you asked), but you can vote as many as you like (regardless of whether you have been accepted or not, regardless of whether you are the author of the question or not).

  • Thank you for acknowledging Gabriel <3. As @hkotsubo said, accepting a response and voting are different things. If an answer related to your question has solved your problem completely, you can accept it. If another answer has also helped you can vote ( it’s basically a stack overflow like rs ). Anyway, I hope you get the 15 points soon and evolve on the kkk platform

Show 2 more comments

0

The problem happens because when the variable cont is equal to b + 1 it continues inside the while loop and gets the value of it minus 1 before having its value printed. To solve this problem, I just added +1 while condition. See the code below:

a = int(input())
b = int(input())
cont = a
if a > b:
    while cont > b + 1:
        cont -= 1
        a -= 1
        print(cont)

You can greatly simplify the code using the function range. See the code below:

a = int(input("Digite o valor de A: "))
b = int(input("Digite o valor de B: "))

for num in range(a-1,b,-1): 
    print(num)

The function range returns an iterable object (sequence) with number of to à b in a step of x.

  • Thank you so much guy, you don’t know how much you helped me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.