Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Looping unknowns with user input hw question
#1
Hello, I am having some difficulty with a problem through Grok Learning. I've been stuck on it for two days now. I think I'm close to a solution, but I'm not getting the in-between outputs as I need to. I tried using the range function but am given coordinates or some reason. So I won't post that code, and will post the one that is closest to what the possible answer could be.

Here is the question;
"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."

What the outputs should to be;
example1
Current floor: 3
Destination floor: 6
Level 3
Level 4
Level 5
Level 6
​
example2
Current floor: 1
Destination floor: 2
Level 1
Level 2
My current answer code is;
floor = int(input("Current floor: "))
dfloor = int(input("Destination floor: "))
while floor <= dfloor:
  if dfloor % floor == 0:      
    print("Level", floor)
  floor = floor + 1
print('')
However, my output comes to just the selected floors;
Current floor: 3
Destination floor: 6
Level 3
Level 6
Quote
#2
This statement is making only certain floors get printed out:
if dfloor % floor == 0:
If you print unconditionally, you'll get the result you want.
Quote
#3
Hello,
why are you using % (modulus) operator? I don't think you need it. It will print only floors satisfying this mathematical condition.
Quote
#4
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:

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
Quote
#5
(Sep-30-2018, 09:06 AM)j.crater Wrote: Hello,
why are you using % (modulus) operator? I don't think you need it. It will print only floors satisfying this mathematical condition.
Hi, I used it because I was modified an example code from the chapter on the Grok learning site. It came close to the answer I needed in contrast to other variations of the code I made. I realize now that it was probably a bad idea and kept me from progression on the assignment.

(Sep-30-2018, 01:13 PM)gruntfutuk Wrote: 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:

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_}')

Thank you so much for this. I ended up changing your suggestion a little but it helped.
Thank you to everyone else as well for your help too~
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Print the longest str from user input edwdas 5 149 Nov-04-2019, 02:02 PM
Last Post: perfringo
  Question about running comparisons through loop from input value Sunioj 2 188 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  how to add user input to a dictionary to a graph KINGLEBRON 3 292 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 346 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 331 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 798 Mar-02-2019, 10:11 PM
Last Post: johneven
  Print The Length Of The Longest Run in a User Input List Ashman111 3 752 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk
  Using user input to restart and quit a game. Maximuskiller78 4 1,357 Sep-17-2018, 04:23 PM
Last Post: gruntfutuk
  Converting user input to integers (retitled) rwood1875 1 919 Jun-17-2018, 04:15 AM
Last Post: Mekire
  Looping through input and using type / isintance to classify it fad3r 7 1,598 Feb-03-2018, 12:56 AM
Last Post: fad3r

Forum Jump:


Users browsing this thread: 1 Guest(s)