0
num_1 = int(input("Enter the first number >>>>"))
num_2 = int(input("Enter the second number >>>>"))

if num_1 > num_2:
    for i in range(num_1,num_2+1):
        print(i)
else:
    for i in range(num_1,num_2,-1):
        print(i)

What is the issue with this code? Once the user inputs the two numbers the program stops and nothing gets printed.

1
  • 1
    You are providing if num1 > num2 then get range(num1, num2) which is not iterating because no elements span from num1 to num2. Example, if 5 > 4.. then range(5,4) doesnt give anything. Commented Jan 8, 2021 at 18:22

5 Answers 5

1

You've got the logic the wrong way around. You need to check is num_1 is less than num_2 when counting up.

num_1 = int(input("Enter the first number >>>>"))
num_2 = int(input("Enter the second number >>>>"))

if num_1 < num_2:
    for i in range(num_1,num_2,+1):
        print(i)
else:
    for i in range(num_1,num_2,-1):
        print(i)
Sign up to request clarification or add additional context in comments.

Comments

0
num_1 = int(input("Enter the first number >>>>"))
num_2 = int(input("Enter the second number >>>>"))

if num_1 < num_2: # in here hace a less then sign and not > 
    for i in range(num_1,num_2+1):
        print(i)
else:
    for i in range(num_1,num_2-1): # In here you had a typo, it had a comma and as a result it was passing in 3 parameters
        print(i)

Comments

0

The issue is your first and second numbers are being used incorrectly in range.

The range takes smaller number first and larger number second. You are providing if num1 > num2 then get range(num1, num2) which is not iterating because no elements span from num1 to num2

num_1 = int(input("Enter the first number >>>>"))
num_2 = int(input("Enter the second number >>>>"))

if num_1 < num_2:
    for i in range(num_1,num_2+1):
        print(i)
else:
    for i in range(num_1,num_2,-1):
        print(i)
Enter the first number >>>>5
Enter the second number >>>>3
5
4

Comments

0

To count from high to low, you need to do more than reverse the step. If you have, for example:

range(1, 10, -1)

This starts at 1, and attempts to count backwards. There are no numbers between 1 and 9 counting backwards from 1 though, so you get an empty collection:

>>> list(range(1, 10, -1))
[]

You need to reverse the arguments as well:

>>> list(range(10, 1, -1))
[10, 9, 8, 7, 6, 5, 4, 3, 2]

Comments

0

you are facing the error because you are using range function in wrong manner.

range(start, stop[, step]) -> range object

Here if step and start value is not provide then start value is consider as 0 by defualt and less than end value. also start is inclusive value and end is exclusive.

so in short range(start, end, step) -> start, start+step, start+step+step, ...., end-1`

in your code,

if num_1 > num_2:
   for i in range(num_1,num_2+1):
     # so on

here you are checking, if num_1>num_2, do for operation on range(num_1. num_2), so since step is not defined by you, it is taken as 1 by default

now the range(num_1, num_2) become [] as num_1 > num_2 and you cant reach num_2 by adding 1 to num_1 continousily. ie so list(range(num_1, num_2)) is [].

since it is empty for loop wont work and it exit the program. same happen (in opposite manner) when num_1<num_2.

to solve this you need to provide the correct value in range or change the if conditions.

so solution be like this

if num_1 < num_2:
    for i in range(num_1,num_2+1):
        print(i)
else:
    for i in range(num_1,num_2,-1):
        print(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.