2

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.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
new
  • 79
  • 2
  • 9

8 Answers8

2

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))
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Thank you so much, it gave me an idea. I just need to create it into a list :) This was the outcome: Current floor: 1 Destination floor: 9 level 1 level [2, 3, 4, 5, 6, 7, 8] level 9 – new Sep 29 '15 at 23:06
  • You are using Python 3 so, if you want it as a list, just call `list(range(current+1, desti))`. – mhawke Sep 29 '15 at 23:07
  • How can I get the output to go downwards? not sideways? – new Sep 29 '15 at 23:17
  • Alright it works, thank you so much :) but sorry to bother you, is there a way to add 'level' infront of the numbers? – new Sep 29 '15 at 23:24
  • There's no need to make a `list` anywhere. Also, the `range` should go from `current` to `desti+1`, not `current+1` to `desti`. – TigerhawkT3 Sep 29 '15 at 23:40
  • Yeah, don't worry I corrected everything and now it works :) Thank you so much for your help guys:)@TigerhawkT3 – new Sep 29 '15 at 23:46
  • @TigerhawkT3: I admit to not answering the OP's assignment question - I have answered the question explicitly asked by the OP, viz how to find the numbers in between. So `current+1 .. desti` correctly answers the OP's question. Secondly, my answer uses `list()` because the OP explicitly requested a list in the first comment to this answer. The last 2 examples where printing actually occurs do not use `list()`. I suggest that you read question, answer and comments more thoroughly before commenting and down-voting (I suppose it was you?). – mhawke Sep 29 '15 at 23:52
  • So, you could clearly see that the OP was going down the wrong paths, but instead of explaining what the right paths are and why, you're just helping him down the wrong paths? – TigerhawkT3 Sep 30 '15 at 00:03
1

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
CodeAJ
  • 110
  • 2
  • 16
Krish
  • 11
  • 1
1
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.

0

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

code123456
  • 98
  • 8
0

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

Achintha Gunasekara
  • 1,165
  • 1
  • 15
  • 29
0

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)
Joel Mac
  • 148
  • 1
  • 10
0

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.

CodeAJ
  • 110
  • 2
  • 16
-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