Reputation: 1
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.
Upvotes: 0
Views: 377
Reputation: 11228
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)
Upvotes: 0
Reputation: 45736
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]
Upvotes: 0
Reputation: 19322
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
Upvotes: 0
Reputation: 400
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)
Upvotes: 0
Reputation: 75
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)
Upvotes: 1