new
new

Reputation: 79

How to find the numbers in between the numbers that the user has input?

I'm using Grok learning and I need help with finding the numbers in between if a user inputs them.

For example:

3
6

The numbers in between: 4, 5

But this is what I need exactly:

Up the lift The lift is broken! It can still go up and down, but doesn't display what floor it's at anymore, which is causing confusion for people trying to use it.

Write a program that will display the floor numbers on an elevator that is going up. Your program should read in the current floor and read in the destination floor, which will always be higher than the current floor. Your program should print out each of the floor numbers in-between.

Current floor: 3

Destination floor: 6

Level 3

Level 4

Level 5

Level 6 ​

Current floor: 1

Destination floor: 2

Level 1

Level 2

So my current code is:

current = int(input("Current floor: "))
desti = int(input("Destination floor: "))
print("level",current)
print("level",desti)

Now I'm confused how to get it to output the numbers in between.

Upvotes: 2

Views: 6155

Answers (8)

user17038142
user17038142

Reputation: 1

here is a simple code that will work

a = int(input("Current floor: "))
b = int(input("Destination floor: "))
i = a
while i < b+1:
  print("Level",i)
  i = i+1

Upvotes: -1

Samuel BROOKES
Samuel BROOKES

Reputation: 11

current = int(input("Current floor: "))
desti = int(input("Destination floor: "))
while current <= desti:
 print("Level " + str(current))
 current = current + 1

Try it with a while loop.

Upvotes: 1

CodeAJ
CodeAJ

Reputation: 110

Here's the sample solution given by Grok Learning itself:

current = int(input('Current floor: '))
destination = int(input('Destination floor: '))
for i in range(current, destination + 1):
  print('Level', i)

Just a tip for future references, in this case, it is necessary to add 1 to the range, because remember, range() starts from 0, because 0 is the very first number. So keep that in mind.

But also, if you would like your code to print your destination floor when you get there, then you can simply add

print(destination)

at the end of your code.

I used this solution, and I found that this was the most simplest way for me, and easy to understand.

I hope this helps you.

Upvotes: 0

Krish
Krish

Reputation: 11

I'm doing the same thing on Grok Learning. Here is a solution:

current = int(input("Current floor: "))
destination = int(input("Destination floor: "))
number = current
while number <= destination:
  print("Level", number)
  number = number + 1

Upvotes: 1

Joel Mac
Joel Mac

Reputation: 148

basically you need to add a range for the # of level here was my answer

current = int(input("Current floor: "))
desti = int(input("Destination floor: "))
for i in range(current,desti):
  print("Level",i)
print("Level",desti)

Upvotes: 0

mhawke
mhawke

Reputation: 87074

You could use the range() function using the upper and lower bounds entered by the user:

>>> current = int(input("Current floor: "))
>>> desti = int(input("Destination floor: "))
>>> print(*range(current+1, desti))
4 5
>>> between = list(range(current+1, desti))
>>> between
[4, 5]

If you want the floors going backwards you can do this:

list(range(desti, current, -1))

or you can simply reverse the range:

list(reversed(range(current+1, desti)))

Printing downwards:

print(*range(current+1, desti), sep='\n')

Formatting the output in a loop:

for level in range(current+1, desti):
    print('Level {}'.format(level))

Upvotes: 2

Achintha Gunasekara
Achintha Gunasekara

Reputation: 1165

Have a look at range function. I think this will do exactly what you want to do.

http://pythoncentral.io/pythons-range-function-explained/

Lets say the range you need is in between 1 and 5.

In Python 2.x it returns a list,

>>> range(1, 5)
[1, 2, 3, 4]

In Python 3.x it's a iterator. so you should convert it to a list.

>>> list(range(1, 5))
[1, 2, 3, 4]

Second input is exclusive, so add one to the last value of the list

4+1=5

Upvotes: 0

code123456
code123456

Reputation: 98

You could create a loop starting at the current floor (exclusive) and increment until you reach the destination floor (exclusive).

Upvotes: 0

Related Questions