Ad

How To Make Loops From Smallest To Largest Number?

- 1 answer

a = int(input("first number: "))
b = int(input("second number: "))
for x in range (a,b):
    print(x, end = ' ')

#I am making a program that would display a number from smallest to largest #for example: the first number is 10 then the second number is 1 : and my expected result is 1,2,3,4,5,6,7,8,9. the program that I did is not working if a is lower than b . but is working if the a is higher than b

Ad

Answer

If you want it to always go from min to max regardless of which one is min and which is max you could do something like this:

for x in range(min(a, b), max(a, b)):
Ad
source: stackoverflow.com
Ad