Home > OS >  When A>B the output is always 1,but when A<B the answer is correct
When A>B the output is always 1,but when A<B the answer is correct

Time:02-23

I already tried changing the values,changing the order,and using if statement,but it all results toa failure

a=int(input())
b=int(input())
list=(range(a,b))
t=1
for i in range(a,b):
t= t * i
print(t)

CodePudding user response:

range() function create a sequence of numbers from A to B, so when A is lower than B your list will be empty and t will stay at 1

CodePudding user response:

A range takes two parameters, a start parameter and an end parameter

print(list(range(1,5)))

Output: [1,2,3,4]

If the start is more than the end, it will result in an empty list print(list(range(4,2))) Output: []

However, range also takes a second parameter called step. The step denotes how you want to get a new element in the range If the step is negative, the range will decrease, printing the desired list

print(list(range(4,2, step=-1)))

Output: [4,3]

  • Related