Sep-30-2018, 01:13 PM
As you know exactly the range required, it is easier to use a for loop than a while loop as it saves you having to write the code for counting.
How about:
If you don't want to use list comprehension, and probably more clear anyway:
How about:
floor = int(input("Current floor: ")) dfloor = int(input("Destination floor: ")) print('\n' + '\n'.join([f'Level {floor_}' for floor_ in range(floor, dfloor + 1)]))This is using list comprehension inside of the print function call to generate a list of strings, each one being "Level " and a floor number. The floor numbers are generated by the for loop using a range starting from one and stopping before the destination floor + 1.
If you don't want to use list comprehension, and probably more clear anyway:
floor = int(input("Current floor: ")) dfloor = int(input("Destination floor: ")) print() for floor_ in range(floor, dfloor + 1): print(f'Level {floor_}')
I am trying to help you, really, even if it doesn't always seem that way